对Javascript中Date对象的扩展
对Javascript中Date日期的扩展,主要是对Date对象扩展了一个格式化日期和解析日期的方法。
/** * 日期解析器 * @param value * @param format * @return {Date} */ Date.prototype.parser = function(value,format) { var shortMonthsToNumber = []; shortMonthsToNumber["Jan"] = "01"; shortMonthsToNumber["Feb"] = "02"; shortMonthsToNumber["Mar"] = "03"; shortMonthsToNumber["Apr"] = "04"; shortMonthsToNumber["May"] = "05"; shortMonthsToNumber["Jun"] = "06"; shortMonthsToNumber["Jul"] = "07"; shortMonthsToNumber["Aug"] = "08"; shortMonthsToNumber["Sep"] = "09"; shortMonthsToNumber["Oct"] = "10"; shortMonthsToNumber["Nov"] = "11"; shortMonthsToNumber["Dec"] = "12"; var parseMonth = function (value) { return shortMonthsToNumber[value] || value; }; var padding = function(value, length) { var paddingCount = length - String(value).length; for(var i = 0 ; i < paddingCount ; i++) { value = "0" + value; } return value; }; var parseTime = function (value) { var retValue = value; var millis = ""; if (retValue.indexOf(".") !== -1) { var delimited = retValue.split('.'); retValue = delimited[0]; millis = delimited[1]; } var values3 = retValue.split(":"); if (values3.length > 0) { var hour = 0,minute = 0,second = 0,millis = 0; var hour = values3[0]; if(values3[1]){ minute = values3[1]; } if(values3[2]){ second = values3[2]; } return { time: retValue, hour: hour, minute: minute, second: second, millis: millis }; } else { return { time: "", hour: "", minute: "", second: "", millis: "" }; } }; var resultDate = new Date(1970, 01, 01, 00, 00, 00); if(!value){ return resultDate; } try { var date = null; var year = null; var month = null; var dayOfMonth = null; var time = null; if (typeof value == "number"){ return this.date(new Date(value), format); } else if (typeof value.getFullYear == "function") { year = value.getFullYear(); month = value.getMonth() + 1; dayOfMonth = value.getDate(); time = parseTime(value.toTimeString()); } else if (value.search(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.?\d{0,3}[Z\-+]?(\d{2}:?\d{2})?/) != -1) { /* 2009-04-19T16:11:05+02:00 || 2009-04-19T16:11:05Z */ var values = value.split(/[T\+-]/); year = values[0]; month = values[1]; dayOfMonth = values[2]; time = parseTime(values[3].split(".")[0]); date = new Date(year, month - 1, dayOfMonth); } else { var values = value.split(" "); switch (values.length) { case 6: /* Wed Jan 13 10:43:41 CET 2010 */ year = values[5]; month = parseMonth(values[1]); dayOfMonth = values[2]; time = parseTime(values[3]); date = new Date(year, month - 1, dayOfMonth); break; case 2: /* 2009-12-18 10:54:50.546 */ var values2 = values[0].split("-"); year = values2[0]; month = values2[1]; dayOfMonth = values2[2]; time = parseTime(values[1]); date = new Date(year, month - 1, dayOfMonth); break; case 7: /* Tue Mar 01 2011 12:01:42 GMT-0800 (PST) */ case 9: /* Fri Apr 08 2011 00:00:00 GMT+0800 (China Standard Time) */ case 10: /* Fri Apr 08 2011 00:00:00 GMT+0200 (W. Europe Daylight Time) */ year = values[3]; month = parseMonth(values[1]); dayOfMonth = values[2]; time = parseTime(values[4]); date = new Date(year, month - 1, dayOfMonth); break; case 1: /* 2012-02-07CET00:00:00 */ var values2 = values[0].split(""); if(values2.length > 3){ year=values2[0]+values2[1]+values2[2]+values2[3]; } if(values2.length > 6){ month= values2[5]+values2[6]; } if(values2.length > 9){ dayOfMonth = values2[8]+values2[9]; } if(values2.length > 20){ time = parseTime(values2[13]+values2[14]+values2[15]+values2[16]+values2[17]+values2[18]+values2[19]+values2[20]); } if(Number(dayOfMonth) && Number(month) && Number(dayOfMonth)){ date = new Date(year, month - 1, dayOfMonth); }else { return new Date(); } break; default: return new Date(); } } var pattern = ""; if(!format){ format = "yyyy-MM-dd HH:mm:ss"; } for (var i = 0; i < format.length; i++) { var currentPattern = format.charAt(i); pattern += currentPattern; switch (pattern) { case "D": case "ddd": pattern = ""; break; case "dd"://月中的某一天。一位数的日期有一个前导零。 if (format.charAt(i + 1) == "d") { break; } resultDate.setDate(Number(padding(dayOfMonth, 2))); pattern = ""; break; case "d"://月中的某一天。一位数的日期没有前导零。 if (format.charAt(i + 1) == "d") { break; } resultDate.setDate(parseInt(dayOfMonth, 10)); pattern = ""; break; case "MMMM"://月份的完整名称,在 MonthNames 中定义。 case "MMM"://月份的缩写名称,在 AbbreviatedMonthNames 中定义。 pattern = ""; break; case "MM"://月份数字。一位数的月份有一个前导零。 if (format.charAt(i + 1) == "M") { break; } resultDate.setMonth(Number(padding(month, 2))-1, 1); pattern = ""; break; case "M"://月份数字。一位数的月份没有前导零。 if (format.charAt(i + 1) == "M") { break; } resultDate.setMonth(parseInt(month, 10)-1, 1); pattern = ""; break; case "y"://不包含纪元的年份。不具有前导零。 case "yyy": if (format.charAt(i + 1) == "y") { break; } pattern = ""; break; case "yy"://不包含纪元的年份。具有前导零。 if (format.charAt(i + 1) == "y" && format.charAt(i + 2) == "y") { break; } resultDate.setFullYear(Number('19'+String(year).slice(-2)), 1, 1); pattern = ""; break; case "yyyy"://包括纪元的四位数的年份。 resultDate.setFullYear(Number(year), 1, 1); pattern = ""; break; case "HH"://24 小时制的小时。一位数的小时数有前导零。 resultDate.setHours(Number(padding(time.hour, 2))); pattern = ""; break; case "H"://24 小时制的小时。一位数的小时数没有前导零。 if (format.charAt(i + 1) == "H") { break; } resultDate.setHours(parseInt(time.hour, 10)); pattern = ""; break; case "hh"://12 小时制的小时。一位数的小时数有前导零。 var hour = (time.hour == 0 ? 12 : time.hour < 13 ? time.hour : time.hour - 12); resultDate.setHours(Number(padding(hour, 2))); pattern = ""; break; case "h"://12 小时制的小时。一位数的小时数没有前导零。 if (format.charAt(i + 1) == "h") { break; } var hour = (time.hour == 0 ? 12 : time.hour < 13 ? time.hour : time.hour - 12); resultDate.setHours(parseInt(hour, 10)); pattern = ""; break; case "mm"://将分钟显示为带前导零的数字 resultDate.setMinutes(Number(padding(time.minute,2))); pattern = ""; break; case "m"://将分钟显示为不带前导零的数字 if (format.charAt(i + 1) == "m") { break; } resultDate.setMinutes(Number(time.minute)); pattern = ""; break; case "ss"://将秒显示为带前导零的数字 resultDate.setSeconds(Number(padding(time.second.substring(0, 2), 2))); pattern = ""; break; case "s"://将秒显示为不带前导零的数字 if (format.charAt(i + 1) == "s") { break; } resultDate.setSeconds(Number(time.second)); pattern = ""; break; default: pattern = ""; break; } } return resultDate; } catch(e) { if(window.console){ console.error(e); } return resultDate; } }