Export (0) Print
Expand All

toJSON Method (Date) (JavaScript)

Used by the JSON.stringify method to enable the transformation of an object's data for JavaScript Object Notation (JSON) serialization.

objectname.toJSON()

objectname

Required. An object for which JSON serialization is wanted.

The toJSON method is used by the JSON.stringify function. JSON.stringify serializes a JavaScript value into JSON text. If a toJSON method is provided to JSON.stringify, the toJSON method is called when JSON.stringify is called.

The toJSON method is a built-in member of the Date JavaScript object. It returns an ISO-formatted date string for the UTC time zone (denoted by the suffix Z).

You can override the toJSON method for the Date type, or define a toJSON method for other object types to achieve transformation of data for the specific object type before JSON serialization.

The following example uses the toJSON method to serialize string member values in uppercase. The toJSON method is called when JSON.stringify is called.

var contact = new Object();
contact.firstname = "Jesper";
contact.surname = "Aaberg";
contact.phone = ["555-0100", "555-0120"];

contact.toJSON = function(key)
 {
    var replacement = new Object();
    for (var val in this)
    {
        if (typeof (this[val]) === 'string')
            replacement[val] = this[val].toUpperCase();
        else
            replacement[val] = this[val]
    }
    return replacement;
};

var jsonText = JSON.stringify(contact);

/* The value of jsonText is:
'{"firstname":"JESPER","surname":"AABERG","phone":["555-0100","555-0120"]}'
*/

The following example illustrates how to use the toJSON method that is a built-in member of the Date object.

var dt = new Date('8/24/2009');
dt.setUTCHours(7, 30, 0);
var jsonText = JSON.stringify(dt);

/* The value of jsonText is:
'"2009-08-24T07:30:00Z"'
*/

Supported in the following document modes: Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information. Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards. Applies To: Date Object

Show:
© 2014 Microsoft