我注意到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()对象支持的格式说明符的文档在哪里?


当前回答

这是一个我经常使用的函数。结果为yyyy-mm-dd hh:mm:ss.nnn。

function date_and_time() {
    var date = new Date();
    //zero-pad a single zero if needed
    var zp = function (val){
        return (val <= 9 ? '0' + val : '' + val);
    }

    //zero-pad up to two zeroes if needed
    var zp2 = function(val){
        return val <= 99? (val <=9? '00' + val : '0' + val) : ('' + val ) ;
    }

    var d = date.getDate();
    var m = date.getMonth() + 1;
    var y = date.getFullYear();
    var h = date.getHours();
    var min = date.getMinutes();
    var s = date.getSeconds();
    var ms = date.getMilliseconds();
    return '' + y + '-' + zp(m) + '-' + zp(d) + ' ' + zp(h) + ':' + zp(min) + ':' + zp(s) + '.' + zp2(ms);
}

其他回答

DateJS当然是功能齐全的,但我推荐这种非常简单的库(JavaScript日期格式),我更喜欢它,因为它只有120行左右。

对于好奇的人来说,有一个名为tc39/temporal的实验特性,目前正处于第3阶段,它为ECMAScript语言带来了一个现代的日期/时间API。

引用tc39网站:

日期一直是ECMAScript中的一个长期难点。这是对Temporal的建议,它是一个充当顶级命名空间(如Math)的全局对象,为ECMAScript语言带来了现代的日期/时间API。要详细了解Date的一些问题以及Temporal的动机,请参阅:Fixing JavaScript Date。

这里有一本烹饪书,可以帮助您开始学习Temporal的来龙去脉。

其他资源:

GitHub存储库-tc39/临时提案YouTube上还有一段视频,详细介绍了这一提议。本文对上述视频进行了简要概述-了解JavaScript如何使用提案时间推进DateTime如何超越时间:使用时态构建未来JavaScript应用程序

我喜欢使用JavaScript和使用日期来格式化时间和日期的10种方法。

基本上,您有三种方法,必须自己组合字符串:

getDate() // Returns the date
getMonth() // Returns the month
getFullYear() // Returns the year

例子:

var d=新日期();var curr_date=d.getDate();var curr_month=d.getMonth()+1//月份为零var curr_year=d.getFullYear();console.log(curr_date+“-”+curr_month+“-“+curr_year);

如果您希望只显示两位数的时间,这可能会帮助您:

var now = new Date();
var cHour = now.getHours();
var cMinuts = now.getMinutes();
var cSeconds = now.getSeconds();

var outStr = (cHour <= 0 ? ('0' + cHour) : cHour) + ':' + (cMinuts <= 9 ? ('0' + cMinuts) : cMinuts) + ':' + (cSeconds <= 9 ? '0' + cSeconds : cSeconds);

答案是“无处”,因为日期格式是专有功能。我不认为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");
}