alert(dateObj)
给了Wed Dec 30 2009 00:00:00 GMT+0800
如何获取2009/12/30
格式的日期
发布于 2010-01-06 21:48:51
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
newdate = year + "/" + month + "/" + day;
或者,您可以设置新的日期并提供上面的值
发布于 2016-02-19 07:59:11
new Date().toISOString()
"2016-02-18T23:59:48.039Z"
new Date().toISOString().split('T')[0];
"2016-02-18"
new Date().toISOString().replace('-', '/').split('T')[0].replace('-', '/');
"2016/02/18"
new Date().toLocaleString().split(',')[0]
"2/18/2016"
发布于 2020-03-10 07:10:43
为什么不将toISOString()
方法与slice
一起使用,或者干脆使用toLocaleDateString()
请查看此处:
const d = new Date() // today, now
console.log(d.toISOString().slice(0, 10)) // YYYY-MM-DD
console.log(d.toLocaleDateString('en-US')) // M/D/YYYY
console.log(d.toLocaleDateString('de-DE')) // D.M.YYYY
console.log(d.toLocaleDateString('pt-PT')) // DD/MM/YYYY
https://stackoverflow.com/questions/2013255
复制相似问题