在哪里可以找到有关在JavaScript中格式化日期的文档? [关闭]

非 Y 不嫁゛ 提交于 2019-12-09 12:15:43

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


#1楼

Moment.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并压缩了文件(2014年2月)


#2楼

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

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

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

例:

var d = new Date(); var curr_date = d.getDate(); var curr_month = d.getMonth() + 1; //Months are zero based var curr_year = d.getFullYear(); console.log(curr_date + "-" + curr_month + "-" + curr_year);


#3楼

JsSimpleDateFormat是一个库,可以格式化日期对象并将已格式化的字符串解析回Date对象。 它使用Java格式(SimpleDateFormat类)。 月份和日期的名称可以本地化。

例:

var sdf = new JsSimpleDateFormat("EEEE, MMMM dd, yyyy");
var formattedString = sdf.format(new Date());
var dateObject = sdf.parse("Monday, June 29, 2009");

#4楼

自定义格式功能:

对于固定格式,只需一个简单的功能即可完成。 以下示例生成国际格式YYYY-MM-DD:

function dateToYMD(date) {
    var d = date.getDate();
    var m = date.getMonth() + 1;
    var y = date.getFullYear();
    return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
}

注意:但是,扩展Javascript标准库通常不是一个好主意(例如,通过将此函数添加到Date的原型中)。

更高级的功能可以根据格式参数生成可配置的输出。 在同一页面上有几个很好的例子。

如果编写格式化函数的时间太长,那么周围有很多函数库。 其他一些答案已经列举出来了。 但是,越来越多的依赖关系也与之相反。

标准ECMAScript格式化功能:

由于ECMAscript是最新版本,因此Date类具有一些特定的格式化功能:

toDateString :与实现有关,仅显示日期。

http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.todatestring

new Date().toDateString(); // eg "Fri Nov 11 2016"

toISOString :显示ISO 8601日期和时间。

http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.toisostring

new Date().toISOString(); // eg "2016-11-21T08:00:00.000Z"

toJSON :JSON的字符串化器。

http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tojson

new Date().toJSON(); // eg "2016-11-21T08:00:00.000Z"

toLocaleDateString :与实现有关,以区域设置格式显示日期。

http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tolocaledatestring

new Date().toLocaleDateString(); // eg "21/11/2016"

toLocaleString :与实现有关,日期和时间为区域设置格式。

http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tolocalestring

new Date().toLocaleString(); // eg "21/11/2016, 08:00:00 AM"

toLocaleTimeString :与实现有关,以语言环境格式表示。

http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tolocaletimestring

new Date().toLocaleTimeString(); // eg "08:00:00 AM"

toString :日期的通用toString。

http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.tostring

new Date().toString(); // eg "Fri Nov 11 2016 08:00:00 GMT+0100 (W. Europe Standard Time)"

注意:可以从这些格式函数中生成自定义输出:

new Date().toISOString().slice(0,10); // By @Image72, return YYYY-MM-DD

#5楼

确保在JavaScript中处理日期时签出Datejs 。 在toString函数的情况下,它令人印象深刻且有据可查。

编辑 :泰勒·福赛斯指出,datejs已过时。 我在当前项目中使用了它,但没有任何问题,但是您应该意识到这一点并考虑替代方法。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!