日期格式更改

时间:2016-07-19 08:50:21

标签: javascript date format

我想在我的代码中更改日期格式 - > 2016年7月19日

有人可以帮助我吗?

function calcWorkingDays(fromDate, days) {
    var count = 0;
    var m = new Date();
    while (count < days) {
        fromDate.setDate(fromDate.getDate() + 1);
        if (fromDate.getDay() != 0 && fromDate.getDay() != 6) // Skip weekends
            count++;
    }
    return fromDate;
}
alert(calcWorkingDays(new Date(), 4));

2 个答案:

答案 0 :(得分:0)

您需要将dateobject格式化为您的肖像:

从DateObject中提取日,月和年:

var month = fromDate.getUTCMonth() + 1; //months from 1-12
//January=0, February=1, etc

var day = fromDate.getUTCDate();
var year = fromDate.getUTCFullYear();

newdate = day + "." + month + "." + year;

完整示例:

function calcWorkingDays(fromDate, days) {
    var count = 0;
    var m = new Date();
    while (count < days) {
        fromDate.setDate(fromDate.getDate() + 1);
        if (fromDate.getDay() != 0 && fromDate.getDay() != 6) // Skip weekends
            count++;
    }

    var month = fromDate.getUTCMonth() + 1;
    var day = fromDate.getUTCDate();
    var year = fromDate.getUTCFullYear();

    newdate = day + "." + month + "." + year;

    return newdate;
}
alert(calcWorkingDays(new Date(), 4));

其他可能的例子是:

new Date().toISOString()
"2016-02-18T23:59:48.039Z"
new Date().toISOString().split('T')[0];
"2016-02-18"
new Date().toISOString().replace('-', '/').split('T')[0].replace('-', '/');
"2016/02/18"

new Date().toLocaleString().split(',')[0]
"2/18/2016"

source

答案 1 :(得分:0)

您应该将日期转换为字符串:

function calcWorkingDays(fromDate, days) {
    var count = 0;
    var m = new Date();
    while (count < days) {
        fromDate.setDate(fromDate.getDate() + 1);
        if (fromDate.getDay() != 0 && fromDate.getDay() != 6) // Skip weekends
        count++;
}
     return fromDate.getDate()  + "." + (fromDate.getMonth()+1) + "." + fromDate.getFullYear();

}

alert(calcWorkingDays(new Date(),4));