如何在JavaScript中获取当前日期?
当前回答
这是一个很好的格式化日期
let date=new date().toLocaleDateString(“en”,{年:“numeric”,日:“2位数”,月:“2位”});console.log(日期);
其他回答
var date = new Date().toLocaleDateString("en-US");
此外,您可以使用两个参数调用方法toLocaleDateString:
var date = new Date().toLocaleDateString("en-US", {
"year": "numeric",
"month": "numeric"
});
有关MDN上此方法的详细信息。
new Date().toDateString();
结果:
“2016年2月3日星期三”
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”
最短的答案是:new Date().toJSON().slice(0,10)
Try
`${Date()}`.slice(4,15)
console.log(`${Date()}`.slice(4,15))
这里我们使用标准的JS功能:模板文本、转换为字符串的Date对象和切片。这可能是满足OP要求的最短解决方案(没有时间,只有日期)