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

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

我注意到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来将日期对象格式化为字符串。


编者注:以下方法是提问者的企图,关于特定浏览器的工作,但也不会在一般的工作; 请参阅此页面上的答案以查看一些实际解决方案。


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


慕容3067478
浏览 513回答 3
3回答

慕丝7291255

我喜欢使用JavaScript和使用日期格式化时间和日期的10种方法。基本上,你有三种方法,你必须自己组合字符串:getDate() // Returns the dategetMonth() // Returns the monthgetFullYear() // Returns the year例:var d = new Date();var curr_date = d.getDate();var curr_month = d.getMonth() + 1; //Months are zero basedvar curr_year = d.getFullYear();console.log(curr_date + "-" + curr_month + "-" + curr_year);
打开App,查看更多内容
随时随地看视频慕课网APP