Unexpected behaviour of javascript timestamp - javascript

I use phoneGap Geo-location API to get current position in which i got time-stamp also.
alert(data[0].timestamp);
alert(new Date(data[0].timestamp).getTime());
where data[0].timestamp = "2013-12-03T19:09:58.859Z"
in web browser the second alert shows 1386097798859 whereas when i built and test app in mobile then it alert as NaN
I don't understand why it happens?

It's a ISODate format. Try to use Date.parse or use momentjs javascript library which can parse a lot of date formats.
Also there is a gist with Date.parse polifyll.

Related

Javascript Moment Invalid Date Error on Internet Explorer 11

I am trying to reformat dates to another language using moment, It works on Chrome and Edge but apparently it is not working on Internet explorer as it returns Invalid Date. Please see code below.
moment.locale('ja);
moment('Jan 05').format('MMM DD');
Yes, creating a moment from a string does not work the same in all browsers. The documentation warns against doing this. If you need consistent parsing, you should instead specify what the format of the string is. For example:
moment('01-05-2021', 'MM-DD-YYYY');
For more information, see documentation here and here

Moment dates are parsed different in Mozilla and Chrome?

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");

Moment.js gives Invalid date in Firefox

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

String to Date mongodb using node.js and moment.js

In my application i allow user to set a date using a date-picker component.
My issue is when trying to convert string date to UTC/ISO date format the conversion is not happening properly, see the below example.
Example:
User Selected String Date : 01/09/2015 (DD/MM/YYYY)
While im using moment.js to convert the above date to native JS date, See below:
moment(req.body.datePicker,'DD/MM/YYYY')
All good till here, but when the data stored in db the date is getting reduced by 1 day. I have no idea where its getting messed up.
I have managed to create a re-producable scenario using jsfiddle-example please have a look for a better understanding.
My assumption:
when i see the db collection the date is getting stored with a default time: 18:30:00.00Z
this could be one of the reason why the dates are getting changed.
Your problem is timezone.
In my case 01/09/2015 gives 2015-01-08T23:00:00 which is only -1 hour since my timezone is GMT+1
Finally after doing some research i myself found the solution or a hack to fix this issue, it may not be the correct way of doing it but for the current scenario it worked like a charm.
Solution:
All i did was just created a full ISO/UTC date string hard-coding the time to zero. moment(req.body.datePicker,'DD/MM/YYYY').format('YYYY-MM-DD HH:mm:ss.000').toString()+'Z' by doing this the default time will always set to Zero and since the string is a fully qualified ISO/UTC standard format, mongoDB will not try to alter it.
Working example:
http://jsfiddle.net/r42jg/1004/
If any one has a better solution, the please post here so we can improvise this better.

Javascript - Retrieve OS Date formatting

Hmmm... Okay so I've been searching the web for 2 days now without any luck. I've seen a lot of answers on how to format a javascript date for example new Date().toString("yyyy-MM-dd")... Which would return something like 2013-04-05.
This is absolutely not the problem.
What I want, is the possibility to set the format in which my OS displays dates, then retrieve that specific format and display it in the browser.
For example, let's say I changed the format of the date in my OS to MM-yyyy/dd (this is for arguement sakes, whether that would work or not is irrelevant)). Then I'd expect to see 04-2013/05 in my OS, right? Next I want to retrieve this specific format in my browser via Javascript so that I can use this to format my dates throughout my webpage.
If this is lunacy and cannot be done, please tell me, as I've got a headache from searching.
Also, if you say use someDateObject.toLocaleDateString() without explaining exactly why .toLocaleDateString would work, I'm going to ignore it, because I've tried setting my date-format in my OS to numerous formats and every single time I use .toLocaleDateString(), I receive the same format: dd/MM/yyyy
first attribute of .toLocaleDateString method locale(s) used.
current locale you can obtain through navigator.language (in firefox or chrome) parameter.
in IE you can obtain navigator.browserLanguage or navigator.systemLanguage
in browsers other than IE it is impossible to obtain system language this way
after this you can call new Date.toLocaleString(navigator.language||navigator.browserLanguage) and it will be formated correctly depending on browser language

Categories