We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

toJSON() 方法返回 Date 对象的字符串形式。

语法

dateObj.toJSON()

描述

Date 实例引用一个具体的时间点。 调用 toJSON() 返回一个 JSON 格式字符串(使用 toISOString()),表示该日期对象的值。默认情况下,这个方法常用于 JSON序列化Date对象。

样例

toJSON() 样例

var date = new Date();
console.log(date); //Thu Nov 09 2017 18:54:04 GMT+0800 (中国标准时间)

var jsonDate = (date).toJSON();
console.log(jsonDate); //"2017-11-09T10:51:11.395Z"

var backToDate = new Date(jsonDate);
console.log(backToDate); //Thu Nov 09 2017 18:54:04 GMT+0800 (中国标准时间)

规范

规范 状态 说明
ECMAScript 5.1 (ECMA-262)
Date.prototype.toJSON
Standard 首次定义,于 JavaScript 1.8.5 版本实现。
ECMAScript 2015 (6th Edition, ECMA-262)
Date.prototype.toJSON
Standard  

浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) 8 (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

相关链接

文档标签和贡献者

此页面的贡献者: KMKNKK, Cattla, helloguangxue, yenshen, Yaty, AlexChao, ziyunfei
最后编辑者: KMKNKK,