function pad(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
}
//yyyyMMdd(),format a Date Object to the format of "yyyyMMdd";
Date.prototype.yyyyMMdd = function () {
var yyyy = this.getFullYear().toString();
var MM = pad(this.getMonth() + 1, 2);
var dd = pad(this.getDate(), 2);
return yyyy + MM + dd;
};
//yyyyMMddHHmmss(),format a Date Object to the format of "yyyyMMddHHmmss";
Date.prototype.yyyyMMddHHmmss = function () {
var yyyy = this.getFullYear().toString();
var MM = pad(this.getMonth() + 1, 2);
var dd = pad(this.getDate(), 2);
var HH = pad(this.getHours(), 2);
var mm = pad(this.getMinutes(), 2);
var ss = pad(this.getSeconds(), 2);
return yyyy + MM + dd + HH + mm + ss;
};
//parseDateWithDate, 格式yyyyMMdd;如果不符合日期格式,就返回undefined
function parseDateWithDate(dateStr) {
// 正则表达式的match数组
var dateMatch;
// validate year as 4 digits, month as 01-12, day as 01-31
if ((dateMatch = dateStr.match(/^(\d{4})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])$/))) {
// make a date
return new Date(+dateMatch[1], +dateMatch[2] - 1, +dateMatch[3]);
}
return undefined;
}
//parseDateWithSecond, 格式yyyyMMddHHmmss;如果不符合日期格式,就返回undefined
function parseDateWithSecond(dateStr) {
// 正则表达式的match数组
var dateMatch;
// validate year as 4 digits, month as 01-12, day as 01-31, hour as 00-23, minute as 00-59, and second as 00-59
if ((dateMatch = dateStr.match(/^(\d{4})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])([0-5]\d)([0-5]\d)$/))) {
// make a date
return new Date(+dateMatch[1], +dateMatch[2] - 1, +dateMatch[3], +dateMatch[4], +dateMatch[5], +dateMatch[6]);
}
return undefined;
}
var d1 = new Date(2013, 0, 1);
var d2 = new Date(2013, 0, 2);
d1 < d2; // true
d1 <= d2; // true
d1 > d2; // false
d1 >= d2; // false
var d1 = new Date(2013, 0, 1);
var d2 = new Date(2013, 0, 1);
/*
* note: d1 == d2 returns false as described above
*/
d1.getTime() == d2.getTime(); // true
d1.valueOf() == d2.valueOf(); // true
Number(d1) == Number(d2); // true
+d1 == +d2; // true