I have a requirement to convert date time in moment.js. But it gives me different result in Chrome and Firefox.
In Google Chrome it gives correct result but in Mozilla firefox gives "Invalid date".
Google chrome
moment('2016-Jan-02 02:00 AM').format()
Output: "2016-01-02T02:00:00+05:30"
Mozilla firefox
moment('2016-Jan-02 02:00 AM').format()
"Invalid date"
Your help is much appreciated.
It's recommended to avoid using moment parsing with custom format. As the documentation states:
Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
In your case, the code for consistent parsing will be:
moment('2016-Jan-02 02:00 AM', 'YYYY-MMM-DD HH:mm A')
You're not specifying a format for parsing the string 2016-Jan-02. So moment falls back to the native Date object, which is inconsistent across the different browsers. To parse the date consistently, include a format string along with it.
e.g.
moment("2016-Jan-02", "DD-MMM-YYYY")
Then if you want to format the moment object as a string, you can do what you were doing before:
moment("2016-Jan-02", "DD-MMM-YYYY").format("DD-MM-YYYY")
which returns the string 02-01-2016 in both browsers.
You need to specify input date format inside moment
moment("input_date_string", "format_of_input_date_string").format("format_of_output_date_string")
eg:
moment("27-06-2022", "DD-MM-YYYY").format("YYYY-MM-DD")
output:
2022-06-27
Related
I have an issue with dates in Mozilla, I am using fullcalendar plugin for angularJs in my project. While using the moment dates, Chrome seems to be working fine but Mozilla seems breaking. I am using Timezones with the moment. Many blogs and posts seems to mention the format of moment dates while having cross browser issues.
Here it is,
console.log(moment(new Date('2017-02-28T18:30:00')).format('dddd-MMM DD,YYYY'));
Mozilla : Tuesday-Feb 28,2017
Chrome : Wednesday-Mar 01,2017
I am really stuck with this, and I have seen post about mentioning the format with the moment dates, but I have used the format in the above sample but it fails to return the desired output. On the other hand Chrome is returning the correct result, what am I doing wrong ?
If you need any details please leave a comment.
Do not use moment(new Date('2017-02-28T18:30:00')) use moment(String) instead (since your input is in ISO 8601 format).
As moment parsing docs says:
Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.
Here a working sample:
console.log(moment('2017-02-28T18:30:00').format('dddd-MMM DD,YYYY'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
If your input is UTC use moment.utc:
moment.utc('2017-02-28T18:30:00')
If your input is in a given timezone (e.g. Asia/Calcutta) use moment.tz(..., String);
moment.tz('2017-02-28T18:30:00', 'Asia/Calcutta')
By default moment parses string in local time, you need to know timezone of your input. Note the moment has local(), utc(), and tz(String) methods to change timezone of a moment object.
You are converting a JS Date to a MomentJS date so I can only assume Firefox parses the JS date differently.
With MomentJS you should be able to simply replace your code:
moment(new Date('2017-02-28T18:30:00')).format('dddd-MMM DD,YYYY')
with:
moment('2017-02-28T18:30:00').format('dddd-MMM DD,YYYY')
https://momentjs.com/docs/#/parsing/
if you use angularjs, it's easier to use filter.
$filter('date')('2017-02-28T18:30:00', "dd/MM/yyyy");
I am creating a new date toISOString -
new Date(03-13-2016 00:00).toISOString();
This works fine in IE and Chrome however NOT in FireFox.
I have tried to modify the string slightly like -
new Date(03-13-2016T00:00:00Z).toISOString();
However this also failed. How can I achieve the desired result to work across all browsers?
2016-03-13T00:00:00.000Z
PS I am aware I start with a string then try and create a string with the toISOString - reason being this handles timezone offset to UTC in one line which is required.
When you pass a string to the Date constructor, it internally calls Date.parse to attempt to get a valid date from it. This first checks to see if it is one of the Date Time formats in the specification. If not (and both "03-13-2016 00:00" and "03-13-2016T00:00:00Z" aren't), the parse specification goes on to say:
If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognizable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.
In this case, it seems both IE and Chrome have code in place that allows it to be correctly parsed, while Firefox doesn't. The only way you're really going to fix this is to have a string that conforms to the specification, or to call the constructor with individual date/time component parts.
If you don't mind pulling a library in or need to work with dates more often, use moment.js which has some very convenient date and time methods and is cross browser compatible.
Your string could then be converted to an ISO String like:
moment('03-13-2016 00:00', 'MM-DD-YYYY HH:mm').format();
I have a very simple operation by converting a string in standard format 1985-10-11T12:23:00 (without timezone specified).
Data.parse() in chrome javascript parses it in Date object as is, but in Firefox a correction for time zone applies.
How I can ensure that in both browsers Date.parse produces result without timezone corrections?
I would suspect
new Date("1985-10-11T12:23:00").toUTCString()
I have figured that out. Date.Parse accepts information about time zone so to ensure cross-browser compartibility, time zone must be added to the zone-less time stamp.
But most commonly used Z+00 notation does not apply. as per Date Parse doc, one has to specify e.g. UTC timezone as +00:00 right after timestamp
I'm facing some date time formatting related issue.
I'm confused about how a date object's output string is formatted. I did some testing in debug, when I call the toLocalString, the output is not following locale settingin the OS .
Below is the output of the method:
"1/12/2015, 8:12:12 PM"
But what I did in the os locale setting is
Why is toLocaleString formatting the date this way? where does those format coming from?
Where to change the format setting browser is using?
Why is toLocaleString formatting the date this way?
toLocaleString() doesn't watch for user's locale formatting settings before returning the string.
Where does those format coming from?
The format is based on the conventions of user's time zone for representing date and time. So, format is machine independent.
Where to change the format setting browser is using?
As stated the format is implementation dependent. It won't help you anything. And I think browsers don't provide such functionality.
For reference I have included it's documentation below.
The Documentation of Date.toLocaleString() as mentioned in Javascript: The Definitive Guide says:
Returns
A string representation of the date and time specified by date. The date and time are repre- sented in the local time zone and formatted using locally appropriate conventions.
Usage
toLocaleString() converts a date to a string, using the local time zone. This method also uses local conventions for date and time formatting, so the format may vary from platform to platform and from country to country. toLocaleString() returns a string formatted in what is likely the user’s preferred date and time format.
If I call this in Firefox (32) or IE (11), it will be parsed as local time:
new Date('2014-10-02T12:00:00')
However, if I parse it in Chrome (37.0.2062.124 m 64-bit), it will be parsed as UTC time.
How can I parse it as local time uniformly across all browsers?
Per ECMA-262, this is, in fact, a valid date format, but a bug may have been the cause for the original discrepant browser behavior.
Try using the time zone indicator from ISO 8601 date standard (append a Z to the end of the timestamp), and see if that helps.
Honorable mention: Moment.js is wonderful for parsing dates, but it’s a hefty lib so that comes with its own trade-offs.