更改日期格式输出

Change dateformat output

本文关键字:输出 格式 日期      更新时间:2023-09-26

我的代码中遇到了一个烦人的问题。

我想让输出日期看起来像这样:

2014/4/6

document.getElementById("nights").value = diff + " nights"; {
    $('#arrival').datepicker().val();
    $('#departure').datepicker().val();
    var start = new Date(document.getElementById('arrival').value),
        end = new Date(document.getElementById('departure').value),
        currentDate = new Date(start),
        between = [];
    while (end > currentDate) {
        between.push(new Date(currentDate));
        currentDate.setDate(currentDate.getDate() + 1);
    }
    $('#lista').html(between.join('<br> '));
};

在哪里可以更改输出格式?

while循环中,尝试:

while (end > currentDate) {
    var month = currentDate.getMonth() + 1;
    var day = currentDate.getDate();
    var year = currentDate.getUTCFullYear();
    between.push(month + '-' + day + '-' + year);
    currentDate.setDate(currentDate.getDate() + 1);
}

看起来您正在做更多的事情:new Date(variable)不必要的。