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


当前回答

如果您正在使用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格式打印当前日期

更清洁、更简单的版本:

new Date().toLocaleString();

结果因用户的语言环境而异:

2017年2月27日上午9:15:41

最短的答案是:new Date().toJSON().slice(0,10)

已缩小2.39KB。一个文件。https://github.com/rhroyston/clock-js只是想帮忙。。。

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;
}};