一、時間格式化(Date原型上添加)this
Date.prototype.format = function() { var s = ''; var mouth = (this.getMonth() + 1)>=10?(this.getMonth() + 1):('0'+(this.getMonth() + 1)); var day = this.getDate()>=10?this.getDate():('0'+this.getDate()); s += this.getFullYear() + '-'; // 獲取年份。 s += mouth + "-"; // 獲取月份。 s += day; // 獲取日。 return (s); // 返回日期。 };
二、獲取兩個日期之間的日期prototype
function getBetweenDate(begin, end) { var dateList = []; var ab = begin.split("-"); var ae = end.split("-"); var db = new Date(); db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]); var de = new Date(); de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]); var unixDb = db.getTime(); var unixDe = de.getTime(); for (var k = unixDb; k <= unixDe;) { //console.log((new Date(parseInt(k))).format()); var timeItem = (new Date(parseInt(k))).format(); dateList.push(timeItem); k = k + 24 * 60 * 60 * 1000; } return dateList; }
三、計算7(n)天前unix
var date = new Date(); var endtime = formatDate(date); date.setTime(date.getTime() - (7-1) * 24 * 60 * 60 * 1000); var time = date.format();