1 | scheduler.date={
|
---|
2 | date_part:function(date){
|
---|
3 | date.setHours(0);
|
---|
4 | date.setMinutes(0);
|
---|
5 | date.setSeconds(0);
|
---|
6 | date.setMilliseconds(0);
|
---|
7 | return date;
|
---|
8 | },
|
---|
9 | time_part:function(date){
|
---|
10 | return (date.valueOf()/1000 - date.getTimezoneOffset()*60)%86400;
|
---|
11 | },
|
---|
12 | week_start:function(date){
|
---|
13 | var shift=date.getDay();
|
---|
14 | if (scheduler.config.start_on_monday){
|
---|
15 | if (shift==0) shift=6
|
---|
16 | else shift--;
|
---|
17 | }
|
---|
18 | return this.date_part(this.add(date,-1*shift,"day"));
|
---|
19 | },
|
---|
20 | month_start:function(date){
|
---|
21 | date.setDate(1);
|
---|
22 | return this.date_part(date);
|
---|
23 | },
|
---|
24 | year_start:function(date){
|
---|
25 | date.setMonth(0);
|
---|
26 | return this.month_start(date);
|
---|
27 | },
|
---|
28 | day_start:function(date){
|
---|
29 | return this.date_part(date);
|
---|
30 | },
|
---|
31 | add:function(date,inc,mode){
|
---|
32 | var ndate=new Date(date.valueOf());
|
---|
33 | switch(mode){
|
---|
34 | case "day": ndate.setDate(ndate.getDate()+inc); break;
|
---|
35 | case "week": ndate.setDate(ndate.getDate()+7*inc); break;
|
---|
36 | case "month": ndate.setMonth(ndate.getMonth()+inc); break;
|
---|
37 | case "year": ndate.setYear(ndate.getFullYear()+inc); break;
|
---|
38 | case "hour": ndate.setHours(ndate.getHours()+inc); break;
|
---|
39 | case "minute": ndate.setMinutes(ndate.getMinutes()+inc); break;
|
---|
40 | default:
|
---|
41 | return scheduler.date["add_"+mode](date,inc,mode);
|
---|
42 | }
|
---|
43 | return ndate;
|
---|
44 | },
|
---|
45 | to_fixed:function(num){
|
---|
46 | if (num<10) return "0"+num;
|
---|
47 | return num;
|
---|
48 | },
|
---|
49 | copy:function(date){
|
---|
50 | return new Date(date.valueOf());
|
---|
51 | },
|
---|
52 | date_to_str:function(format,utc){
|
---|
53 | format=format.replace(/%[a-zA-Z]/g,function(a){
|
---|
54 | switch(a){
|
---|
55 | case "%d": return "\"+scheduler.date.to_fixed(date.getDate())+\"";
|
---|
56 | case "%m": return "\"+scheduler.date.to_fixed((date.getMonth()+1))+\"";
|
---|
57 | case "%j": return "\"+date.getDate()+\"";
|
---|
58 | case "%n": return "\"+(date.getMonth()+1)+\"";
|
---|
59 | case "%y": return "\"+scheduler.date.to_fixed(date.getFullYear()%100)+\"";
|
---|
60 | case "%Y": return "\"+date.getFullYear()+\"";
|
---|
61 | case "%D": return "\"+scheduler.locale.date.day_short[date.getDay()]+\"";
|
---|
62 | case "%l": return "\"+scheduler.locale.date.day_full[date.getDay()]+\"";
|
---|
63 | case "%M": return "\"+scheduler.locale.date.month_short[date.getMonth()]+\"";
|
---|
64 | case "%F": return "\"+scheduler.locale.date.month_full[date.getMonth()]+\"";
|
---|
65 | case "%h": return "\"+scheduler.date.to_fixed((date.getHours()+11)%12+1)+\"";
|
---|
66 | case "%g": return "\"+((date.getHours()+11)%12+1)+\"";
|
---|
67 | case "%G": return "\"+date.getHours()+\"";
|
---|
68 | case "%H": return "\"+scheduler.date.to_fixed(date.getHours())+\"";
|
---|
69 | case "%i": return "\"+scheduler.date.to_fixed(date.getMinutes())+\"";
|
---|
70 | case "%a": return "\"+(date.getHours()>11?\"pm\":\"am\")+\"";
|
---|
71 | case "%A": return "\"+(date.getHours()>11?\"PM\":\"AM\")+\"";
|
---|
72 | case "%s": return "\"+scheduler.date.to_fixed(date.getSeconds())+\"";
|
---|
73 | case "%W": return "\"+scheduler.date.to_fixed(scheduler.date.getISOWeek(date))+\"";
|
---|
74 | default: return a;
|
---|
75 | }
|
---|
76 | })
|
---|
77 | if (utc) format=format.replace(/date\.get/g,"date.getUTC");
|
---|
78 | return new Function("date","return \""+format+"\";");
|
---|
79 | },
|
---|
80 | str_to_date:function(format,utc){
|
---|
81 | var splt="var temp=date.split(/[^0-9a-zA-Z]+/g);";
|
---|
82 | var mask=format.match(/%[a-zA-Z]/g);
|
---|
83 | for (var i=0; i<mask.length; i++){
|
---|
84 | switch(mask[i]){
|
---|
85 | case "%j":
|
---|
86 | case "%d": splt+="set[2]=temp["+i+"]||1;";
|
---|
87 | break;
|
---|
88 | case "%n":
|
---|
89 | case "%m": splt+="set[1]=(temp["+i+"]||1)-1;";
|
---|
90 | break;
|
---|
91 | case "%y": splt+="set[0]=temp["+i+"]*1+(temp["+i+"]>50?1900:2000);";
|
---|
92 | break;
|
---|
93 | case "%g":
|
---|
94 | case "%G":
|
---|
95 | case "%h":
|
---|
96 | case "%H":
|
---|
97 | splt+="set[3]=temp["+i+"]||0;";
|
---|
98 | break;
|
---|
99 | case "%i":
|
---|
100 | splt+="set[4]=temp["+i+"]||0;";
|
---|
101 | break;
|
---|
102 | case "%Y": splt+="set[0]=temp["+i+"]||0;";
|
---|
103 | break;
|
---|
104 | case "%a":
|
---|
105 | case "%A": splt+="set[3]=set[3]%12+((temp["+i+"]||'').toLowerCase()=='am'?0:12);";
|
---|
106 | break;
|
---|
107 | case "%s": splt+="set[5]=temp["+i+"]||0;";
|
---|
108 | break;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | var code ="set[0],set[1],set[2],set[3],set[4],set[5]";
|
---|
112 | if (utc) code =" Date.UTC("+code+")";
|
---|
113 | return new Function("date","var set=[0,0,1,0,0,0]; "+splt+" return new Date("+code+");");
|
---|
114 | },
|
---|
115 |
|
---|
116 | getISOWeek: function(ndate) {
|
---|
117 | if(!ndate) return false;
|
---|
118 | var nday = ndate.getDay();
|
---|
119 | if (nday == 0) {
|
---|
120 | nday = 7;
|
---|
121 | }
|
---|
122 | var first_thursday = new Date(ndate.valueOf());
|
---|
123 | first_thursday.setDate(ndate.getDate() + (4 - nday));
|
---|
124 | var year_number = first_thursday.getFullYear(); // year of the first Thursday
|
---|
125 | var ordinal_date = Math.floor( (first_thursday.getTime() - new Date(year_number, 0, 1).getTime()) / 86400000); //ordinal date of the first Thursday - 1 (so not really ordinal date)
|
---|
126 | var week_number = 1 + Math.floor( ordinal_date / 7);
|
---|
127 | return week_number;
|
---|
128 | },
|
---|
129 |
|
---|
130 | getUTCISOWeek: function(ndate){
|
---|
131 | return this.getISOWeek(ndate);
|
---|
132 | }
|
---|
133 | }
|
---|