我注意到JavaScript的新Date()函数在接受多种格式的日期方面非常聪明。

Xmas95 = new Date("25 Dec, 1995 23:15:00")
Xmas95 = new Date("2009 06 12,12:52:39")
Xmas95 = new Date("20 09 2006,12:52:39")

调用new Date()函数时,我在任何地方都找不到显示所有有效字符串格式的文档。

这用于将字符串转换为日期。如果我们从相反的方面来看,即将日期对象转换为字符串,直到现在,我的印象是JavaScript没有将日期对象格式化为字符串的内置API。

编者按:以下方法是询问者在特定浏览器上的尝试,但通常不起作用;请参阅本页上的答案以了解一些实际解决方案。

今天,我在date对象上使用了toString()方法,令人惊讶的是,它可以将日期格式化为字符串。

var d1 = new Date();
d1.toString('yyyy-MM-dd');       //Returns "2009-06-29" in Internet Explorer, but not Firefox or Chrome
d1.toString('dddd, MMMM ,yyyy')  //Returns "Monday, June 29,2009" in Internet Explorer, but not Firefox or Chrome

在这里,我也找不到任何关于将日期对象格式化为字符串的方法的文档。

列出Date()对象支持的格式说明符的文档在哪里?


当前回答

只是为了继续龚志涛的坚定回答-这处理上午/下午

 Date.prototype.format = function (format) //author: meizz
{
    var hours = this.getHours();
    var ttime = "AM";
    if(format.indexOf("t") > -1 && hours > 12)
    {
        hours = hours - 12;
        ttime = "PM";
     }

var o = {
    "M+": this.getMonth() + 1, //month
    "d+": this.getDate(),    //day
    "h+": hours,   //hour
    "m+": this.getMinutes(), //minute
    "s+": this.getSeconds(), //second
    "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
    "S": this.getMilliseconds(), //millisecond,
    "t+": ttime
}

if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
  (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o) if (new RegExp("(" + k + ")").test(format))
    format = format.replace(RegExp.$1,
      RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
return format;
}

其他回答

您引用的功能不是标准的Javascript,不太可能跨浏览器移植,因此不是很好的实践。ECMAScript 3规范将解析和输出格式功能留给Javascript实现。ECMAScript 5添加了ISO8601支持的子集。我相信你提到的toString()函数是一个浏览器的创新(Mozilla?)

有几个库提供了参数化这一点的例程,其中一些库提供了广泛的本地化支持。您还可以查看dojo.date.local中的方法。

动量.js

它是一个(轻量级)*JavaScript日期库,用于解析、处理和格式化日期。

var a = moment([2010, 1, 14, 15, 25, 50, 125]);
a.format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"
a.format("ddd, hA");                       // "Sun, 3PM"

(*)轻量级意味着在尽可能小的设置中缩小9.3KB+gzip(2014年2月)

答案是“无处”,因为日期格式是专有功能。我不认为toString函数要符合特定的格式。例如在ECMAScript 5.1规范中(http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf,2013年2月8日,第173页),toString函数记录如下:

“字符串的内容取决于实现”

下面的示例等函数可以很容易地完成格式化。

function pad(toPad, padWith) {
    return (String(padWith) + String(toPad)).slice(-1 * padWith.length);
}

function dateAsInputValue(toFormat) {
    if(!(toFormat instanceof Date)) return null;
    return toFormat.getFullYear() + "-" + pad(toFormat.getMonth() + 1, "00") + "-" + pad(toFormat.getDate(), "00");
}

function timeAsInputValue(toFormat) {
    if(!(toFormat instanceof Date)) return null;        
    return pad(toFormat.getHours(), "00") + ":" + pad(toFormat.getMinutes(), "00") + ":" + pad(toFormat.getSeconds(), "00");
}

我找不到任何关于有效日期格式的权威文档,所以我编写了自己的测试,看看各种浏览器支持什么。

http://blarg.co.uk/blog/javascript-date-formats

我的结果得出结论,以下格式在我测试的所有浏览器中都有效(示例使用日期“2013年8月9日”):

[完整年份]/[月份]/[日期编号]-月份可以是带或不带前导零的数字,也可以是短格式或长格式的月份名称,日期编号可以带或不带有前导零。

2013/08/092013/08/92013/8/092013/8/92013/08/092013年8月9日2013年8月09日2013年8月9日

【月份】/【全年】/【日期编号】-月份可以是带或不带前导零的数字,也可以是短格式或长格式的月份名称,日期编号可以带或不带有前导零。

08/2013/0908/2013/98/2013/098/2013/92013/09年8月2013年8月/9日2013年8月09日2013年8月/9日

用空格分隔的[整年]、[月名]和[日期编号]的任意组合-月名可以是短格式或长格式,日期编号可以是带或不带前导零。

2013年8月9日2013年8月09日2013年8月9日2013年8月9日2013年8月9日2013年8月9日等

也适用于“现代浏览器”(或除IE9及以下版本外的所有浏览器)

[全年]-[月号]-[日期号]-月号和日期号必须包含前导零(这是MySQL日期类型使用的格式)

2013-08-09

使用月份名称:有趣的是,在使用月份名称时,我发现只有月份名称的前3个字符被使用过,因此以下所有字符都是完全有效的:

new Date('9 August 2013');
new Date('9 Aug 2013');
new Date('9 Augu 2013');
new Date('9 Augustagfsdgsd 2013');

使用此函数

toTimeString() and toLocaleDateString()

有关详细信息,请参阅下面的链接https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date