Is the output of Date.toString() always in the same format? - javascript

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.

Related

How to get a timestamp in UTC but without the time?

I need to generate a date in UTC format, but without the time part, that is, instead of getting:
Wed, 19 Jan 2021 19:27:00 GMT
I need only:
Wed, 19 Jan 2021
I know that there is a "long" way to do it by formatting the string myself using methods one instance of Date() and concatenating them. But I would like to know if there is a shorter way to do it.
By the way, I can only use Javascript vanilla.
I don't know what you mean by long but the code for extracting only the part you're looking for is pretty short.
'Wed, 19 Jan 2021 19:27:00 GMT'.split(' ').slice(0, -2)
Edit: If you really dislike this code I just found out that Date.toDateString() exists
By using substring:
let d = new Date();
d.toString().substring(0,15)

Chrome 66: Date.parse() gives unexpected results for invalid ISO 8601 dates

Output from Chrome 66 debug console with Hongkong timezone:
Valid date:
new Date('2018-06-30')
Sat Jun 30 2018 08:00:00 GMT+0800 (China Standard Time)
Invalid date gives T+1 value!
new Date('2018-06-31')
Sun Jul 01 2018 08:00:00 GMT+0800 (China Standard Time)
Finally... and invalid date error.
new Date('2018-06-32')
Invalid Date
Why does June 31 give T+1?
The JavaScript Date object is happy to handle unit rollover for you, that's all that's going on with the 2018-06-31 example — it's handling the rollover from June 30th to July 1st.
It doesn't do that for the 2018-06-32 example because 32 is an invalid value for the days field (whereas 31 isn't, it's just that June only has 30 days). The spec defines the valid ranges for the parts of the date/time string here, where we can see it says the valid values for the day of the month are 01 to 31 inclusive.
It's probably worth noting that the parsing of that ISO-8601-derived format (it isn't ISO-8601, quite) if you don't include a timezone indicator has a checkered history, sadly. ES5 specified ISO-8601 but got the meaning of the absense of a timezone indicator wrong (it said it should mean UTC, but ISO-8601 says it means local time); then ES2015 tried to fix that, but conforming to ES2016's rules would have broken a substantial amount of real-world code; and so it wasn't stabilized until ES2016, which says: Date-only forms (like yours) without a timezone indicator are UTC, date/time forms without one are local time. (It's been fine for years if you do include a timezone indicator.)

Getting IST Time stamp using JavaScript

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.

Firefox vs Chrome Date Difference with momentJS

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

Invalid date/None showing up for JS Date object in IE/Firefox

I am displaying a date time object in a table however for some reason in IE it display as None or Invalid Date is there something wrong with my format or is there an easy way for making this more readable such as mm/dd/yyyy HH:MM
this is what displays in chrome:
Mon Nov 28 2011 16:00:00 GMT-0500 (EST)
This is being converted from a Unix timestamp to that output in an API layer.
Probably the creation of the Date object fails, because the new Date() constructor accepts just some limited, implementation-dependent set of date strings.
You can use the Globalize library to deal with such issues, even if no localization in the usual sense is involved—but dealing with different string presentations of dates as is localization of a kind. It first looks a bit messy (it takes some time to dig into it—my book “Going Global with JavaScript and Globalize.js” contains a more readable description of it, with many examples), and it’s far from perfect, but it’s very useful.
If you know that your timestamp data is of some known exact format, you can parse it easily and then output it according to your own format descriptor. Assuming, for the sake of definiteness, that the format is the one exemplified with
Mon Nov 28 2011 16:00:00 GMT-0500 (EST)
(I know it’s an output format you mentioned, but I just use it as an example), you would first do simple string operation to discard the “GMT” and “(EST)” part (Globalize cannot currently handle them), producing e.g.
Mon Nov 28 2011 16:00:00 -05:00
and then you would just use code like the following:
var foo = Globalize.parseDate(timestamp,'ddd MMM d yyyy HH:mm:ss zzz');
var out = Globalize.format(foo,'MM/dd/yyyy HH:MM');
document.write(out);
just make your own method to format the date as string so you pass all problems with diffrent browsers and plateforms
I suspect Chrome is being helpful here and calling the .toString() method for you.
The Date object has several methods for formatting string output. See the w3schools reference page for examples.

Categories

Resources