如何在JavaScript中获取当前日期?
当前回答
// Try this simple way
const today = new Date();
let date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
console.log(date);
其他回答
您可以使用moment.js:http://momentjs.com/
var m=moment().格式(“DD/MM/YYYY”);文件.写入(m);<script src=“https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js“></script>
Date.prototype.toLocalFullDateStringYYYYMMDDHHMMSS = function () {
if (this != null && this != undefined) {
let str = this.getFullYear();
str += "-" + round(this.getMonth() + 1);
str += "-" + round(this.getDate());
str += "T";
str += round(this.getHours());
str += ":" + round(this.getMinutes());
str += ":" + round(this.getSeconds());
return str;
} else {
return this;
}
function round(n){
if(n < 10){
return "0" + n;
}
else return n;
}};
var utc=new Date().toJSON().slice(0,10).replace(/-/g,'/');文档.写入(utc);
如果要重用utc变量,例如new Date(utc),请使用替换选项,因为Firefox和Safari无法识别带破折号的日期。
如果您正在使用jQuery。试试这一行:
$.datepicker.formatDate('dd/mm/yy', new Date());
以下是格式化日期的惯例
d-月份的日期(无前导零)dd-月份的日期(两位数)o-一年中的某一天(无前导零)oo-一年中的一天(三位数)D-天名称缩写DD-日名称长m-一年中的月份(无前导零)mm-一年中的月份(两位数)M-月名缩写MM-月份名称长y-年(两位数)yy-年(四位数)
以下是jQuery日期选择器的参考
这可能会帮助你
var date = new Date();
console.log(date.getDate()+'/'+(date.getMonth()+1)+'/'+date.getFullYear());
这将以dd/MM/yyyy格式打印当前日期