I have a function that gets the date in the DATETIME format:
2015-06-18 00:00:00
Doing moment.utc("2015-06-18 00:00:00").toDate() will display different results in Firefox and Chrome:
Firefox: Date 2015-06-18T00:00:00.000Z
Chrome: Thu Jun 18 2015 03:00:00 GMT+0300 (EEST)
Also, using new Date("2015-06-18 00:00:00") will return Invalid Date in Firefox, but adding a "T" before the hours will fix that issue. But then if I do new Date("2015-06-18T00:00:00") will return:
Firefox: Date 2015-06-17T21:00:00.000Z
Chrome: Thu Jun 18 2015 03:00:00 GMT+0300 (EEST)
It's driving me nuts.
How can I get both browsers to show the same hour?
How can I get Firefox to display the result in Chrome's format?
The whole point of using a dedicated date library is to obtain transparent cross-browser date features. However, as soon as you run .toDate() you get back the native Date object. If you then convert it to string by using the builtin Date.toString() method you've finally dropped all the library goodies and got back to vanilla JavaScript.
Tips:
Don't use strings expect for display purposes
Use the library features to generate those strings
try
new Date("2015-06-18T00:00:00").toString()
looks the same on both for me
Related
I wish to get the following timestamp as output :
Wed Jan 03 2018 22:35:29 GMT+0530 (IST)
When I tried using the following code in browser,
var d = new Date();
Am getting the desired result. But whenever I run the snippet in my Webstorm and console the result, I am getting the result as
2018-01-03T17:11:53.093Z
Why such difference in format? Why is the result produced in the browser not reproducing in my IDE Console? How to produce the result in the exact format as
Wed Jan 03 2018 22:35:29 GMT+0530 (IST)
I think that's something that the JavaScript engine does built inside of the IDE. Anyways the raw data when you console.log the date object isn't directly used generally so that is why there are differences in how different engines may output it.
The IDE's JavaScript engine is most certainly outputting the date object similar to when it is output using DateObject.toISOString() in a browser which produces something like 2018-01-03T17:55:37.048Z
To fix that you can tell it to output it differently, like this
var d = new Date();
console.log( d.toString() );
I tried it on Node and seems to work pretty well, the IDE is probably using Node so that's going to work fine with it as well.
OR Use momentjs or a similar library to normalize how you want to output a time object
While you are getting the output in the format
Wed Jan 03 2018 22:35:29 GMT+0530 (IST)
I am getting it as
Wed Jan 03 2018 23:01:41 GMT+0530 (India Standard Time)
Not much different, still you can see the difference in how IST is shown. Probably browsers also take the settings of your operating system in consideration when outputting a raw time object.
Hello All, I have time in milliseconds and I want convert it into "dd/mm/yyyy hh:mm:ss"
I have tried the following code in javascript but I failed to do it, What I get is "Thu Jul 07 2016 16:22:10 GMT+0530 (IST)" this full timestamp, Code I tried is as follows:
self.users[i].lastLoggedIn = (new Date(self.users[i].lastLoggedIn)).toString("mm/dd/yyyy hh:mm:ss");
Where am I going wrong? for reference I have attached one screen shot of the same. Thank you..!!!
I suggest using moment.js for handling dates in Javascript. It is available for browsers, server-side engines and so on.
moment(self.users[i].lastLoggedIn).format("mm/dd/yyyy hh:mm:ss")
As the title asks, is the output of Date.toString() (more precisely, Date.toTimeString()) always in the same format, in all browsers?
I ask this since the EMCAScript specification says that the "contents of the String are implementation-dependent".
I need to make sure of this because I need to format the string by inserting HTML span elements as follows:
(new Date()).toTimeString().replace(" GMT", "<span id='offset'> GMT") + '</span>' );
This would produce (in Google Chrome v28.0.1500.95) something like
18:19:26<span id="offset"> GMT-0700 (Pacific Daylight Time)</span>
which I can then style with CSS.
Suggestions for better ways to style the output would also be great!
In brief, no, toString does not always output the same format.
new Date().toString()
prints
"Sat Feb 11 2012 02:15:10 GMT+0100" in Firefox 10
"Sat Feb 11 2012 02:18:29 GMT+0100 (W. Europe Standard Time)" in Chrome 16
"Sat Feb 11 02:18:59 UTC+0100 2012" in Internet Explorer 9
I know these are older browser version, but it shows that it is browser dependent.
However, when using toTimeString() it appears it always starts with hours:minutes:seconds[space]...
Therefore, you could instead split the string into 2 portions based on the first [space] with:
indexOf(" ")
Then wrap the second portion with your span
Moment does some string formatting of dates, but it also does not handle your requested offset string very well as it depends on the toString() method.
I hope that helps
Moment.js is a nice library for formatting dates in JS. https://github.com/moment/moment
Otherwise yes, you'll end up parsing it yourself as browsers render this differently.
I am trying to convert a datetime (raw format in UTC: 2012-12-05T21:55:00) to readable local time format but the output varies across browsers:
DateTime = new Date(DateTime);
alert(DateTime);
In Chrome it appears as:
Wed Feb 20 2013
05:31:00 GMT +0800
(Malay Peninsula
Standard Time)
and in IE:
Wed Feb 20 05:31:00
UTC+0800 2013
Hence, I applied a .format to it:
DateTime = new Date(DateTime);
DateTime.format('dd MMM, yyyy hh:mm tt')
alert(DateTime);
This format appears good and consistent across Chrome and IE9:
20 Feb, 2013 05:31 AM
but there's an issue in displaying it in IE8 and Safari, it shows the date as:
NaN, 000NaN
NaN:NaN AM
I tried other JS plugins like date ninja but no help either. Can anyone advise how to go about the NaN issue? Thanks.
I faced 'exactly' the same issue you are facing a few months back. Use moment.js. It gives consistent date formats in Chrome, FF and IE(Including IE 8)
So now, its 9:23am. I have a UTC date string that represents the current date, that looks like this "2012-07-17T09:23:27.75"
I want that in a date object, so I can display a nicely formatted date, so I:
var myDate = new Date("2012-07-17T09:23:27.75")
// Gives --> Tue Jul 17 2012 10:23:27 GMT+0100 (GMT Daylight Time)
So because of daylight saving time I'm getting an hour-out issue. I can see that myDate.getTimezoneOffset() gives me -60, what's the standard / best practice way to get my date to actually reflect the current correct time? Have I just entered javascript date hell?
Try momentjs.com. I really found it handy for such things.
var myDate = moment("2012-07-17T09:23:27.75");
Gives you a date instance in your timezone (that basically configured on your computer). Moreover momentjs has nice human friendly formattings like "a couple of seconds ago", "a month ago",...
Dates are really a hell in JS (but not only in JS). The best thing you can do is to always only transport in UTC between browser <-> server. Then on the server convert it to what time format you like, you obviously only have to be consistent. That way I managed to handle date-times properly.
Try removing the 'T'
I was debugging some date time format issue in chrome when I found out that in console
new Date('2016-04-16T15:15:00') returns Sat Apr 16 2016 16:15:00 GMT+0100 (GMT Daylight Time)
while
new Date('2016-04-16 15:15:00') returns Sat Apr 16 2016 15:15:00 GMT+0100 (GMT Daylight Time)