JavaScript计算日期的函数

注意:Date对象获得的月份是从0开始记的,这个让我耽误了不少时间:

 

function addDays(nd){
    var day = new Date();
    var newtimems = day.getTime() + (nd * 24 * 60 * 60 * 1000);
    day.setTime(newtimems);
    var xyear = day.getFullYear();
    var xmonth = day.getMonth()+1 < 10? "0"+ (day.getMonth() + 1):""+(day.getMonth() + 1);
    var xday = day.getDate() < 10? "0"+day.getDate():""+day.getDate();
    return xyear + "-" + xmonth + "-" + xday;
}

你可能感兴趣的:(JavaScript,Date,function)