-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdate.min.js
19 lines (19 loc) · 4.06 KB
/
date.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
* Date prototype extensions. Doesn't depend on any
* other code. Doens't overwrite existing methods.
*
* Adds dayNames, abbrDayNames, monthNames and abbrMonthNames static properties and isLeapYear,
* isWeekend, isWeekDay, getDaysInMonth, getDayName, getMonthName, getDayOfYear, getWeekOfYear,
* setDayOfYear, addYears, addMonths, addDays, addHours, addMinutes, addSeconds methods
*
* Copyright (c) 2006 Jörn Zaefferer and Brandon Aaron ([email protected] || http://brandonaaron.net)
*
* Additional methods and properties added by Kelvin Luck: firstDayOfWeek, dateFormat, zeroTime, asString, fromString -
* I've added my name to these methods so you know who to blame if they are broken!
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
Date.dayNames=['日','一','二','三','四','五','六'];Date.abbrDayNames=['日','一','二','三','四','五','六'];Date.monthNames=['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'];Date.abbrMonthNames=['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'];Date.firstDayOfWeek=1;Date.format='yyyy-mm-dd';Date.fullYearStart='20';(function(){function add(a,b){if(!Date.prototype[a]){Date.prototype[a]=b}};add("isLeapYear",function(){var y=this.getFullYear();return(y%4==0&&y%100!=0)||y%400==0});add("isWeekend",function(){return this.getDay()==0||this.getDay()==6});add("isWeekDay",function(){return!this.isWeekend()});add("getDaysInMonth",function(){return[31,(this.isLeapYear()?29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()]});add("getDayName",function(a){return a?Date.abbrDayNames[this.getDay()]:Date.dayNames[this.getDay()]});add("getMonthName",function(a){return a?Date.abbrMonthNames[this.getMonth()]:Date.monthNames[this.getMonth()]});add("getDayOfYear",function(){var a=new Date("1/1/"+this.getFullYear());return Math.floor((this.getTime()-a.getTime())/86400000)});add("getWeekOfYear",function(){return Math.ceil(this.getDayOfYear()/7)});add("setDayOfYear",function(a){this.setMonth(0);this.setDate(a);return this});add("addYears",function(a){this.setFullYear(this.getFullYear()+a);return this});add("addMonths",function(a){var b=this.getDate();this.setMonth(this.getMonth()+a);if(b>this.getDate())this.addDays(-this.getDate());return this});add("addDays",function(a){this.setTime(this.getTime()+(a*86400000));return this});add("addHours",function(a){this.setHours(this.getHours()+a);return this});add("addMinutes",function(a){this.setMinutes(this.getMinutes()+a);return this});add("addSeconds",function(a){this.setSeconds(this.getSeconds()+a);return this});add("zeroTime",function(){this.setMilliseconds(0);this.setSeconds(0);this.setMinutes(0);this.setHours(0);return this});add("asString",function(a){var r=a||Date.format;return r.split('yyyy').join(this.getFullYear()).split('yy').join((this.getFullYear()+'').substring(2)).split('mmmm').join(this.getMonthName(false)).split('mmm').join(this.getMonthName(true)).split('mm').join(j(this.getMonth()+1)).split('dd').join(j(this.getDate())).split('hh').join(j(this.getHours())).split('min').join(j(this.getMinutes())).split('ss').join(j(this.getSeconds()))});Date.fromString=function(s,a){var f=a||Date.format;var d=new Date('01/01/1977');var b=0;var c=f.indexOf('mmmm');if(c>-1){for(var i=0;i<Date.monthNames.length;i++){var e=s.substr(c,Date.monthNames[i].length);if(Date.monthNames[i]==e){b=Date.monthNames[i].length-4;break}}d.setMonth(i)}else{c=f.indexOf('mmm');if(c>-1){var e=s.substr(c,3);for(var i=0;i<Date.abbrMonthNames.length;i++){if(Date.abbrMonthNames[i]==e)break}d.setMonth(i)}else{d.setMonth(Number(s.substr(f.indexOf('mm'),2))-1)}}var g=f.indexOf('yyyy');if(g>-1){if(c<g){g+=b}d.setFullYear(Number(s.substr(g,4)))}else{if(c<g){g+=b}d.setFullYear(Number(Date.fullYearStart+s.substr(f.indexOf('yy'),2)))}var h=f.indexOf('dd');if(c<h){h+=b}d.setDate(Number(s.substr(h,2)));if(isNaN(d.getTime())){return false}return d};var j=function(a){var s='0'+a;return s.substring(s.length-2)}})();