Hello any one having idea why i am getting "invalid date" in Safari, in other browsers works as expected.
moment('01/01/2023 11:44:00.000 AM').tz(time_zone, true).format('hh:mm:ss:SS A z')
chrome
safari
I have tried something like
.replace(/ /g,"T")
but no luck any idea what to try?
Look like this will work, provide date format as second param.
moment('01/01/2023 11:44:00.000 AM', 'MM/dd/yyyy HH:mm:ss.SSS`').tz(time_zone, true).format('hh:mm:ss:SS A z')
Related
DayJs
Using it on the browser if that matters (firefox + Vue + typescript).
This is my date string
2021-02-05 12:00 AM
It fusses about the AM/PM in my code:
const dateObj: any = dayjs('2021-02-05 12:00 AM').format('YYYY-MM-DD hh:mm A');
The output of dateObj is always "Invalid Date". If I remove the "AM" from the string it parses correctly. If I try this online tester for the same code, the output is
NaN-NaN-NaN NaN:NaN PM
Like with my dev environment, if I remove the AM, it's fine.
Any ideas?
EDIT: Working on Chrome and not Firefox...
Issue on Firefox
If you deeply look at the implementation, you would see above day string going through the Day constructor: new Day('2021-02-05 12:00 AM'). Unfortunately FF doesn't support this support this day string format, but Chrome does.
Best approach
The dayjs documentation mentions:
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
If you're still keen to use above format, you would have to use a plugin as mentioned here
Basically, you have to change as below to work in all browsers:
import customParseFormat from 'dayjs/plugin/customParseFormat'
import dayjs from "dayjs"
dayjs.extend(customParseFormat)
const yourDate = dayjs('2021-02-05 12:00 AM', 'YYYY-MM-DD HH:mm A')
I am fetching a date object from an API. Here's the format returned from the API:
"2016-04-05 13:39:46.612"
Chrome seems to interpret this and display it correctly, but in Firefox, Safari, and IE, I get either null or invalid date.
Chrome results:
console.log(purchaseDate)
>> Tue Apr 05 2016 13:39:46 GMT-0500 (CDT)
This HTML in Angular view: {{$scope.purchaseDate | date:'MMM d, y h:mm a'}}
outputs: Apr 5, 2016 1:39 PM
Firefox results (similar for Safari and IE):
console.log(purchaseDate)
>> Invalid Date
This HTML in Angular view: {{$scope.purchaseDate | date:'MMM d, y h:mm a'}}
outputs: null
I've tried formatting the date using moment.js before sending it to the browser (as suggested in answers to similar questions), but the results aren't much better:
Chrome results:
console.log(moment(purchaseDate).format())
>> 2016-04-05T13:39:46-05:00
Firefox results (similar for Safari and IE):
console.log(moment(purchaseDate).format())
>> Invalid Date
Any ideas? I'd like to use moment.js to get the parsing consistent, since I'm already using it elsewhere in my code.
If you're going to use moment with angular, you should also use angular-moment.
When loading your value, create a moment object instead of a Date object.
var purchaseDate = moment("2016-04-05 13:39:46.612");
This particular format is already recognized by moment.js, so you don't need to provide a format string when parsing. Though do be aware that it will be interpreted as local time. If you wanted it in UTC instead, then:
var purchaseDate = moment.utc("2016-04-05 13:39:46.612");
Either way, you can then use angular-moment to format it:
{{$scope.purchaseDate | amDateFormat:'MMM D, Y h:mm a'}}
Angular-moment also provide lots of other filters you can use if you like. Check out their docs.
I think I had a similar issue and for me the best solution was to use momentjs library to parse the date string:
bower install --save moment
and then for example:
$scope.purchaseDate = moment("2016-04-05 13:39:46.612", "YYYY-MM-DD HH:mm:ss.SSS").toDate();
see parsing format here: http://momentjs.com/docs/#/parsing/string-format/
in the HTML page for example:
{{purchaseDate | date:'medium'}}
beware that momentjs takes care of your local timezone and will the date accordingly change
I hope it helps.
I have rendered the normal text with jquery date rule. The date rule working fine in chrome but not working in the Firefox and IE . please see my code block.
<form id="myform">
<input id="datepick" type="text" name ="datepick"/>
<input type="submit" id="ValidateDate" />
</form>
$("#myform").validate({
rules: {
datepick:{
date:"MM/dd/yyyy"
}
},
messages: {
datepick:{
date:"Give MM/dd/yyyy format"
}
}
});
$('#datepick').keyup(function () {
$("#ValidateDate").submit();
});
</script>
when i type 12/3err/3001 it throws error in all browser
when i type 12/234/2333 it throws error chrome not in firefox and IE.
Additionally i want to share one information.
http://jqueryvalidation.org/date-method/
In the above link jQuery validation is not working properly in Mozilla , IE browser even for a normal textbox.
please type the 22/233/2222 value in the above jquery link sample then you can find the below output variation
In chrome :
In Firefox:
Please help me to resolve this.....
Thanks,
Gobalakrishnan
The documentation you've linked to says this:
This method should not be used, since it relies on the new Date constructor, which behaves very differently across browsers and locales. Use dateISO instead or one of the locale specific methods (in localizations/ and additional-methods.js).
My emphasis.
This issue is due to the date parsing behavior difference between the browsers. You can see the below.
Chrome
new Date("92/12/2015")
- Invalid Date
FireFox
new Date("92/12/2015")
- Date {Sun Jun 07 1998 00:00:00 GMT+0530 (India Standard Time)}
The date rule will only check for format based on inputted date value and hence the validation succeeded in FF.
So as #SomekidwithHTML proposed you can use the dateIso which will check the date against ISO date standards or implement your our custom validation as per your need.
http://jqueryvalidation.org/jQuery.validator.addMethod/
The page works fine in Chrome, but I have this one minor error in Firefox and a different problem in IE. Assistance with either of these issues is greatly appreciated. Since I've been stumped in the Firefox error the longest, I'll start with that one:
Here's the code: http://truxmapper.appspot.com/sched.html
The date picker selects a date using the format "07-08-2010 23:28". Now, I need to pass this time as a parameter to my servlet, which is expecting the time represented as a long. This is not a problem in Chrome. The Date object accepts a string in the format given above, but when I try to use getTime() on a date instantiated with a string in Firefox, it returns NaN. So what I've done in the on the page I linked to is a little handling asking the user to re-enter the dates if its read as NaN. This obviously isn't even a band-aid solution since even if you re-enter the date its still going to read NaN. I need to know why the Date function wont instantiate using the string you see in the input text field in Firefox.
In IE, for some reason its telling me that sTime is undefined.
That date format is ambiguous. Try it as yyyy-mm-dd instead of mm-dd-yyyy or dd-mm-yyyy.
Try
new Date(Date(dateString)).getTime()
(feels like an ugly workaround...)
Edit: This will produce wrong result.
The date format used in Javascript should be of the form YYYY MM DD HH:mm:ss. You can convert the format into this form with
// dateString = "07-08-2010 23:28";
dateString = dateString.replace(/(\d+) (\d+) (\d+)/, '$3-$1-$2');
But as mentioned in the comment, there is no standard Date format used by Javascript before the ECMAScript 5 standard. It is better to parse the dateString directly:
m = dateString.match(/(\d+)-(\d+)-(\d+) (\d+):(\d+)/)
date = new Date(+m[3], m[1]-1, +m[2], +m[4], +m[5]); // Note: January = 0.
I've been trying to debug a script of mine and I can't get my head around what's wrong with this:
var date = new Date("19871104071535".replace(
/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
'$4:$5:$6 $2/$3/$1'
));
alert(date);
It works in Firefox, IE, Chrome but Safari gives me an "Invalid Date" error. Any ideas?
The Time and Date are in the wrong order (for just Safari I guess :):
I tested this in Safari and it works (I just swapped Date and Time position in the final string):
var date = new Date("19871104071535".replace(
/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
'$2/$3/$1 $4:$5:$6'
));
alert(date);
It will also work in the other browsers because this is what is expected.