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


当前回答

(function() { var d = new Date(); return new Date(d - d % 86400000); })()

其他回答

像这样漂亮地打印日期。

2015年6月1日上午11:36:48

https://gist.github.com/Gerst20051/7d72693f722bbb0f6b58

new Date().toISOString().slice(0,10); 

也会起作用

Try

`${Date()}`.slice(4,15)

console.log(`${Date()}`.slice(4,15))

这里我们使用标准的JS功能:模板文本、转换为字符串的Date对象和切片。这可能是满足OP要求的最短解决方案(没有时间,只有日期)

我的解决方案使用字符串文字。了解更多信息。。。

//声明日期为dvar d=新日期()//日期的内联格式const exampleOne=`${d.getDay()}-${d.getMonth()+1}-${d.getFullYear()}`//一月为0,因此需要+1//使用特征线和运算符常量示例二=`+++++++++++带换行符和算术运算符示例新行上的年份:${d.getFullYear()}年份减(-)30年:${d.getFullYear()-30}你明白了。。。+++++++++++`console.log('============')console.log(示例一)console.log('============')console.log(示例二)

var utc=new Date().toJSON().slice(0,10).replace(/-/g,'/');文档.写入(utc);

如果要重用utc变量,例如new Date(utc),请使用替换选项,因为Firefox和Safari无法识别带破折号的日期。