如何在JavaScript中获取当前日期?
当前回答
// Try this simple way
const today = new Date();
let date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
console.log(date);
其他回答
更清洁、更简单的版本:
new Date().toLocaleString();
结果因用户的语言环境而异:
2017年2月27日上午9:15:41
实现这一点的简单方法(同时考虑到您当前的时区,它利用了ISO yyyy-mm-dd格式)是:
let d = new Date().toISOString().substring(0,19).replace("T"," ") // "2020-02-18 16:41:58"
通常,这是一种非常通用的兼容日期格式,如果需要,可以将其转换为纯日期值:
Date.parse(d); // 1582044297000
(function() { var d = new Date(); return new Date(d - d % 86400000); })()
new Date().toDateString();
结果:
“2016年2月3日星期三”
我的方式
let dateString = new Date().toLocaleString().split(',').find(() => true);