首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

date.toString

toString() 方法返回一个字符串,表示该Date对象。

语法

代码语言:javascript
复制
dateObj.toString()

返回值

代表给定日期的字符串。

描述

Date对象覆盖了 Object对象的toString() 方法;它不是继承自Object.prototype.toString()。对于  Date对象,toString()方法返回一个表示该对象的字符串。

toString 方法总是返回一个美式英语日期格式的字符串。

toString()当日期被表示为文本值时,或者在字符串连接中引用日期时,JavaScript会自动调用该方法。

toString() 是通用函数。如果不是Date实例,则 返回"Invalid Date"。

例子

Using toString()

下例把一个Date对象的 toString 返回值赋给 myVar:

代码语言:javascript
复制
var x = new Date();
myVar = x.toString(); // assigns a value to myVar similar to:
// Mon Sep 28 1998 14:36:22 GMT-0700 (PDT)

规格

Specification

Status

Comment

ECMAScript 1st Edition (ECMA-262)

Standard

Initial definition. Implemented in JavaScript 1.0.

ECMAScript 5.1 (ECMA-262)The definition of 'Date.prototype.toLocaleTimeString' in that specification.

Standard

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Date.prototype.toString' in that specification.

Standard

ECMAScript Latest Draft (ECMA-262)The definition of 'Date.prototype.toString' in that specification.

Draft

浏览器兼容性

Feature

Chrome

Firefox

Edge

Internet Explorer

Opera

Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

Feature

Android

Chrome for Android

Edge mobile

Firefox for Android

IE mobile

Opera Android

iOS Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

扫码关注腾讯云开发者

领取腾讯云代金券