XIANFESchool / FE-problem-collection

前端问题收集和知识经验总结
https://github.com/ShuyunXIANFESchool/FE-problem-collection/issues
63 stars 22 forks source link

JavaScript中常见的日期计算 #3

Open hjzheng opened 8 years ago

hjzheng commented 8 years ago
// 当前日期减3个月
var current = new Date();
current.setMonth(current.getMonth() - 3);
// 参数 年,月(0-11)
function getDaysInMonth(year, month) {
    var days_in_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if (month === 1) {
        return ((year % 4 === 0) && ((year % 100) !== 0)) || (year % 400 === 0) ? 29 : 28;
    } else {
        return days_in_months[month];
    }
}
 var EndTime = new Date('2014/12/20 00:00:00');
 var NowTime = new Date();
 var t = EndTime.getTime() - NowTime.getTime();
 var d = Math.floor(t/1000/60/60/24); // 天
 var h = Math.floor(t/1000/60/60%24); // 小时
 var m = Math.floor(t/1000/60%60); // 分钟
 var s = Math.floor(t/1000%60); //秒

其他相关资料 lishengzxc/bblog/issues/5 Calculating Dates and Times (JavaScript)