9

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!

Alfred Xing
  • 4,406
  • 2
  • 23
  • 34
  • 2
    You already got an answer! It's *implementation-dependent* as you mentioned. So I suggest you not write code based on current implementation's result. – Mics Aug 02 '13 at 02:19
  • Although I am late to answer this, I will leave my comments for the benefit of others who land on this question seeking an answer:- The spec is clear about this. It reads "The contents of the String from toString() are implementation-dependent," (Refer: ecma-international.org/ecma-262/5.1/#sec-15.9.5.2) If you have to stringize date thena a better alternative is to use toISOString(), which as per the spec is strictly "YYYY-MM-DDTHH:mm:ss.sssZ" formatted. (Refer: ecma-international.org/ecma-262/5.1/#sec-15.9.5.43) – Ashwin Prabhu Jun 17 '15 at 11:05

2 Answers2

5

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

PostureOfLearning
  • 3,481
  • 3
  • 27
  • 44
  • Thanks for the in-depth answer. I'll probably be creating my own date formatter in JavaScript then; Moment.js is a bit too heavy (some features I probably won't use). – Alfred Xing Aug 02 '13 at 03:19
  • 1
    The spec is clear about this. It reads "The contents of the String from toString() are implementation-dependent," (Refer: http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5.2) If you have to stringize date thena a better alternative is to use toISOString(), which as per the spec is strictly "YYYY-MM-DDTHH:mm:ss.sssZ" formatted. (Refer: http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5.43) – Ashwin Prabhu Jun 17 '15 at 11:01
-1

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.

Kelly
  • 7
  • 1