我试图使用JS将日期对象转换为YYYYMMDD格式的字符串。有没有比连接Date.getYear(), Date.getMonth()和Date.getDay()更简单的方法?


当前回答

我写了一个简单的函数,它可以将Date对象转换为具有日期号、月份号(带零填充)和年份号的可定制顺序的String。您可以将它与您喜欢的任何分隔符一起使用,或者将此参数保留为空以在输出中不显示分隔符。请看一看。

function dateToString(date, $1, $2, $3, separator='') { const dateObj = { date: String(date.getDate()).padStart(2, '0'), month: String(date.getMonth() + 1).padStart(2, '0'), year: date.getFullYear() }; return dateObj[$1] + separator + dateObj[$2] + separator + dateObj[$3]; } const date = new Date(); const dateString1 = dateToString(date, 'year', 'month', 'date'); console.log(dateString1); // Manipulate arguments order to get output you want const dateString2 = dateToString(date, 'date', 'month', 'year', '-'); console.log(dateString2);

其他回答

Try this:

函数showdate () {

var a = new Date();
var b = a.getFullYear();
var c = a.getMonth();
(++c < 10)? c = "0" + c : c;
var d = a.getDate();
(d < 10)? d = "0" + d : d;
var final = b + "-" + c + "-" + d; 

return final;


} 

document.getElementById("todays_date").innerHTML = showdate();

一个衬线(2022),带正确的时区偏移

var dateDisplay = new Date(Date.now() - (new Date().getTimezoneOffset() * 1000 * 60)).toJSON().slice(0, 10).replaceAll("-", "");

// YearMonthDay var dateDisplay = new Date(Date.now() - (new Date().getTimezoneOffset() * 1000 * 60)).toJSON().slice(0, 10).replaceAll("-", ""); console.log("YearMonthDay"); console.log(dateDisplay); // Year-Month-Day var dateDisplay = new Date(Date.now() - (new Date().getTimezoneOffset() * 1000 * 60)).toJSON().slice(0, 10); console.log("Year-Month-Day"); console.log(dateDisplay); // Year-Month-Day Hour:Minute:Second var dateDisplay = new Date(Date.now() - (new Date().getTimezoneOffset() * 1000 * 60)).toJSON().slice(0, 19).replace("T", " "); console.log("Year-Month-Day Hour:Minute:Second"); console.log(dateDisplay); // Year-Month-Day Hour-Minute-Second var dateDisplay = new Date(Date.now() - (new Date().getTimezoneOffset() * 1000 * 60)).toJSON().slice(0, 19).replace("T", " ").replaceAll(":", "-"); console.log("Year-Month-Day Hour-Minute-Second"); console.log(dateDisplay); // ISO-8601 standard: YYYY-MM-DDTHH:mm:ss.sssZ var dateDisplay = new Date(Date.now() - (new Date().getTimezoneOffset() * 1000 * 60)).toJSON(); console.log("ISO-8601 standard: YYYY-MM-DDTHH:mm:ss.sssZ"); console.log(dateDisplay);

回答另一个简单性和可读性的问题。 此外,不鼓励使用新方法编辑现有的预定义类成员:

function getDateInYYYYMMDD() {
    let currentDate = new Date();

    // year
    let yyyy = '' + currentDate.getFullYear();

    // month
    let mm = ('0' + (currentDate.getMonth() + 1));  // prepend 0 // +1 is because Jan is 0
    mm = mm.substr(mm.length - 2);                  // take last 2 chars

    // day
    let dd = ('0' + currentDate.getDate());         // prepend 0
    dd = dd.substr(dd.length - 2);                  // take last 2 chars

    return yyyy + "" + mm + "" + dd;
}

var currentDateYYYYMMDD = getDateInYYYYMMDD();
console.log('currentDateYYYYMMDD: ' + currentDateYYYYMMDD);

有些日期=新日期(); =某物。 游戏机。log (dateFormated);

这段代码修复了Pierre Guilbert的答案:

(10000年后依然有效)

YYYYMMDD=new Date().toISOString().slice(0,new Date().toISOString().indexOf("T")).replace(/-/g,"")