Bug 29063 - JSON.stringify(Date) loses the milliseconds information
Summary: JSON.stringify(Date) loses the milliseconds information
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: Mac (Intel) OS X 10.5
: P3 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-09-08 18:37 PDT by Shumpei Shiraishi
Modified: 2009-09-09 00:10 PDT (History)
1 user (show)

See Also:


Attachments
Test case for this bug. (750 bytes, text/html)
2009-09-08 18:37 PDT, Shumpei Shiraishi
no flags Details
Patch v1 (5.72 KB, patch)
2009-09-08 23:33 PDT, Oliver Hunt
mjs: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Shumpei Shiraishi 2009-09-08 18:37:36 PDT
Created attachment 39241 [details]
Test case for this bug.

I tried to use the JSON.stringify() for Date object on Safari4 and
WebKit nightly build(48096), so I got result as follows.

> JSON.stringify(new Date());
> 2009-09-07T04:49:43Z

This result seems that milliseconds information of date is lost.
For some applications, millis info is important and this behavior may
be problem.

And in the ECMAScript 5th specification, Date.toISOString() (this
function is called from Date.prototype.toJSON()) is defined as:

...Date Time string format (15.9.1.15). *All fields are present* in
the string...

So, I think the Webkit's behavior should be treated as a bug, isn't it?

P.S.
Current implementations' status is as follows (tested by attached test case):

Firefox3.5: keep milliseconds
Webkit: lost milliseconds
Opera10: not yet implemented
IE8: lost milliseconds

Mmm... IE8's behavior is same as Webkit.
Is current behavior collect? Is JSON.stringify() useless for apps which want to treat the information of milliseconds?
Comment 1 Oliver Hunt 2009-09-08 18:55:23 PDT
> Mmm... IE8's behavior is same as Webkit.
> Is current behavior collect? Is JSON.stringify() useless for apps which want to
> treat the information of milliseconds?
Per spec it is incorrect, but that said, toJSON is a function like any other, eg.
Date.prototype.toJSON = function() {
   ...
}
Comment 2 Shumpei Shiraishi 2009-09-08 20:20:04 PDT
> Per spec it is incorrect, but that said, toJSON is a function like any other,
> eg.
> Date.prototype.toJSON = function() {
>    ...
> }

OK, I see.

And I wrote a JS library for ISO-8601 date format. It is useful to implement the custom Date.prototype.toJSON().

http://wiki.github.com/shumpei/iso8601.js/
Comment 3 Oliver Hunt 2009-09-08 23:33:46 PDT
Created attachment 39250 [details]
Patch v1
Comment 4 Maciej Stachowiak 2009-09-09 00:00:43 PDT
Comment on attachment 39250 [details]
Patch v1

r=me
Comment 5 Oliver Hunt 2009-09-09 00:10:30 PDT
Landed r48202