如何在JavaScript中获取当前日期?


当前回答

每次都可以:

var now=新日期();var day=(“0”+now.getDate()).slice(-2);var month=(“0”+(现在.getMonth()+1)).sslice(-2);var today=now.getFullYear()+“-”+(月)+“”+(日);console.log(今天);

其他回答

var d=(new Date()).toString().split('').spling(1,3).join('');文档.写入(d)

要将其分解为步骤:

(new Date()).toString()给出“2013年6月28日星期五15:30:18 GMT-0700(PDT)”(new Date()).toString().split(“”)将上述字符串除以每个空格,并返回一个数组,如下所示:[“Fri”,“Jun”,”28“,”2013“,”15:31:14“,”GMT-0700“,”(PDT)“](new Date()).toString().split('').spling(1,3).join('')从上述数组中获取第二、第三和第四个值,用空格将它们连接起来,并返回字符串“Jun 28 2013”

每次都可以:

var now=新日期();var day=(“0”+now.getDate()).slice(-2);var month=(“0”+(现在.getMonth()+1)).sslice(-2);var today=now.getFullYear()+“-”+(月)+“”+(日);console.log(今天);

new Date().toDateString();

结果:

“2016年2月3日星期三”

要获取日期,则将其内置到JavaScript中:

new Date();

如果您正在寻找日期格式,并且无论如何都在为您的网站使用Kendo jQuery UI库,那么我建议使用内置的Kendo函数:

kendo.toString(new Date(), "yyMMdd"); // Or any other typical date format

有关支持格式的完整列表,请参阅此处。

这做了很多:

var today=新日期();var date=today.getFullYear()+'/'+(today.get-Month()+1)+'/'+today.get date();文档.写入(日期);

其中today.getFullYear()获取当前年份。

today.getMonth()+1获取当前月份。

today.getDate()获取今天的日期。

所有这些都用“/”连接。