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


当前回答

试用Date.js

毫秒

date.js.millisecond(); // 0.00

date.js.second(); // 58

分钟

date.js.minute(); // 31

小时

date.js.hour(); // 6  (PM)

Days

date.js.day(); // Monday

date.js.week(); // (Week Of the Month / WOM) => 2

date.js.month(); // (Month) => November

TLM(三字母月)

date.js.tlmonth(); // (Month) => Dec

Year

date.js.year(); // (Year / String: "") => "2021"

date.js.season(); // (Fall / Season: seasons) => "fall"

当前时间(AM/PM)

date.js.time(); // (Time / Zone: "PDT/EDT etc.") => 10:04 AM

其他回答

我的方式

let dateString = new Date().toLocaleString().split(',').find(() => true);

你可以用这个

<script>
function my_curr_date() {      
    var currentDate = new Date()
    var day = currentDate.getDate();
    var month = currentDate.getMonth() + 1;
    var year = currentDate.getFullYear();
    var my_date = month+"-"+day+"-"+year;
    document.getElementById("dateField").value=my_date;    
}
</script>

HTML是

<body onload='return my_curr_date();'>
    <input type='text' name='dateField' id='dateField' value='' />
</body>

您可以签出此

var today = new Date();
today = parseInt(today.getMonth()+1)+'/'+today.getDate()+'/'+today.getFullYear()+"\nTime : "+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds();
document.write(today);

请参阅Date()构造函数的文档。链接

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

这做了很多:

var today=新日期();var date=today.getFullYear()+'/'+(today.get-Month()+1)+'/'+today.get date();文档.写入(日期);

其中today.getFullYear()获取当前年份。

today.getMonth()+1获取当前月份。

today.getDate()获取今天的日期。

所有这些都用“/”连接。