Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / templates / munstrap / static / js / formatdate.js @ 8589c6df

Historique | Voir | Annoter | Télécharger (12,4 ko)

1 a0f3aded Jonny McCullagh
// formatDate :
2
// a PHP date like function, for formatting date strings
3
// authored by Svend Tofte <www.svendtofte.com>
4
// the code is in the public domain
5
//
6
// see http://www.svendtofte.com/javascript/javascript-date-string-formatting/
7
// and http://www.php.net/date
8
//
9
// thanks to 
10
//  - Daniel Berlin <mail@daniel-berlin.de>,
11
//    major overhaul and improvements
12
//  - Matt Bannon,
13
//    correcting some stupid bugs in my days-in-the-months list!
14
//  - levon ghazaryan. pointing out an error in z switch.
15
//  - Andy Pemberton. pointing out error in c switch 
16
//
17
// input : format string
18
// time : epoch time (seconds, and optional)
19
//
20
// if time is not passed, formatting is based on 
21
// the current "this" date object's set time.
22
//
23
// supported switches are
24
// a, A, B, c, d, D, F, g, G, h, H, i, I (uppercase i), j, l (lowecase L), 
25
// L, m, M, n, N, O, P, r, s, S, t, U, w, W, y, Y, z, Z
26
// 
27
// unsupported (as compared to date in PHP 5.1.3)
28
// T, e, o
29
30
Date.prototype.formatDate = function (input,time) {
31
    
32
    var daysLong =    ["Sunday", "Monday", "Tuesday", "Wednesday", 
33
                       "Thursday", "Friday", "Saturday"];
34
    var daysShort =   ["Sun", "Mon", "Tue", "Wed", 
35
                       "Thu", "Fri", "Sat"];
36
    var monthsShort = ["Jan", "Feb", "Mar", "Apr",
37
                       "May", "Jun", "Jul", "Aug", "Sep",
38
                       "Oct", "Nov", "Dec"];
39
    var monthsLong =  ["January", "February", "March", "April",
40
                       "May", "June", "July", "August", "September",
41
                       "October", "November", "December"];
42
43
    var switches = { // switches object
44
        
45
        a : function () {
46
            // Lowercase Ante meridiem and Post meridiem
47
            return date.getHours() > 11? "pm" : "am";
48
        },
49
        
50
        A : function () {
51
            // Uppercase Ante meridiem and Post meridiem
52
            return (this.a().toUpperCase ());
53
        },
54
    
55
        B : function (){
56
            // Swatch internet time. code simply grabbed from ppk,
57
            // since I was feeling lazy:
58
            // http://www.xs4all.nl/~ppk/js/beat.html
59
            var off = (date.getTimezoneOffset() + 60)*60;
60
            var theSeconds = (date.getHours() * 3600) + 
61
                             (date.getMinutes() * 60) + 
62
                              date.getSeconds() + off;
63
            var beat = Math.floor(theSeconds/86.4);
64
            if (beat > 1000) beat -= 1000;
65
            if (beat < 0) beat += 1000;
66
            if ((String(beat)).length == 1) beat = "00"+beat;
67
            if ((String(beat)).length == 2) beat = "0"+beat;
68
            return beat;
69
        },
70
        
71
        c : function () {
72
            // ISO 8601 date (e.g.: "2004-02-12T15:19:21+00:00"), as per
73
            // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
74
            return (this.Y() + "-" + this.m() + "-" + this.d() + "T" + 
75
                    this.H() + ":" + this.i() + ":" + this.s() + this.P());
76
        },
77
        
78
        d : function () {
79
            // Day of the month, 2 digits with leading zeros
80
            var j = String(this.j());
81
            return (j.length == 1 ? "0"+j : j);
82
        },
83
        
84
        D : function () {
85
            // A textual representation of a day, three letters
86
            return daysShort[date.getDay()];
87
        },
88
        
89
        F : function () {
90
            // A full textual representation of a month
91
            return monthsLong[date.getMonth()];
92
        },
93
        
94
        g : function () {
95
           // 12-hour format of an hour without leading zeros, 1 through 12!
96
           if (date.getHours() == 0) {
97
               return 12;
98
           } else {
99
               return date.getHours()>12 ? date.getHours()-12 : date.getHours();
100
           }
101
       },
102
        
103
        G : function () {
104
            // 24-hour format of an hour without leading zeros
105
            return date.getHours();
106
        },
107
        
108
        h : function () {
109
            // 12-hour format of an hour with leading zeros
110
            var g = String(this.g());
111
            return (g.length == 1 ? "0"+g : g);
112
        },
113
        
114
        H : function () {
115
            // 24-hour format of an hour with leading zeros
116
            var G = String(this.G());
117
            return (G.length == 1 ? "0"+G : G);
118
        },
119
        
120
        i : function () {
121
            // Minutes with leading zeros
122
            var min = String (date.getMinutes ());
123
            return (min.length == 1 ? "0" + min : min);
124
        },
125
        
126
        I : function () {
127
            // Whether or not the date is in daylight saving time (DST)
128
            // note that this has no bearing in actual DST mechanics,
129
            // and is just a pure guess. buyer beware.
130
            var noDST = new Date ("January 1 " + this.Y() + " 00:00:00");
131
            return (noDST.getTimezoneOffset () == 
132
                    date.getTimezoneOffset () ? 0 : 1);
133
        },
134
        
135
        j : function () {
136
            // Day of the month without leading zeros
137
            return date.getDate();
138
        },
139
        
140
        l : function () {
141
            // A full textual representation of the day of the week
142
            return daysLong[date.getDay()];
143
        },
144
        
145
        L : function () {
146
            // leap year or not. 1 if leap year, 0 if not.
147
            // the logic should match iso's 8601 standard.
148
            // http://www.uic.edu/depts/accc/software/isodates/leapyear.html
149
            var Y = this.Y();
150
            if (         
151
                (Y % 4 == 0 && Y % 100 != 0) ||
152
                (Y % 4 == 0 && Y % 100 == 0 && Y % 400 == 0)
153
                ) {
154
                return 1;
155
            } else {
156
                return 0;
157
            }
158
        },
159
        
160
        m : function () {
161
            // Numeric representation of a month, with leading zeros
162
            var n = String(this.n());
163
            return (n.length == 1 ? "0"+n : n);
164
        },
165
        
166
        M : function () {
167
            // A short textual representation of a month, three letters
168
            return monthsShort[date.getMonth()];
169
        },
170
        
171
        n : function () {
172
            // Numeric representation of a month, without leading zeros
173
            return date.getMonth()+1;
174
        },
175
        
176
        N : function () {
177
            // ISO-8601 numeric representation of the day of the week
178
            var w = this.w();
179
            return (w == 0 ? 7 : w);
180
        },
181
        
182
        O : function () {
183
            // Difference to Greenwich time (GMT) in hours
184
            var os = Math.abs(date.getTimezoneOffset());
185
            var h = String(Math.floor(os/60));
186
            var m = String(os%60);
187
            h.length == 1? h = "0"+h:1;
188
            m.length == 1? m = "0"+m:1;
189
            return date.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
190
        },
191
        
192
        P : function () {
193
            // Difference to GMT, with colon between hours and minutes
194
            var O = this.O();
195
            return (O.substr(0, 3) + ":" + O.substr(3, 2));
196
        },      
197
        
198
        r : function () {
199
            // RFC 822 formatted date
200
            var r; // result
201
            //  Thu         ,     21               Dec              2000
202
            r = this.D() + ", " + this.d() + " " + this.M() + " " + this.Y() +
203
            //    16          :    01          :    07               0200
204
            " " + this.H() + ":" + this.i() + ":" + this.s() + " " + this.O();
205
            return r;
206
        },
207
208
        s : function () {
209
            // Seconds, with leading zeros
210
            var sec = String (date.getSeconds ());
211
            return (sec.length == 1 ? "0" + sec : sec);
212
        },        
213
        
214
        S : function () {
215
            // English ordinal suffix for the day of the month, 2 characters
216
            switch (date.getDate ()) {
217
                case  1: return ("st"); 
218
                case  2: return ("nd"); 
219
                case  3: return ("rd");
220
                case 21: return ("st"); 
221
                case 22: return ("nd"); 
222
                case 23: return ("rd");
223
                case 31: return ("st");
224
                default: return ("th");
225
            }
226
        },
227
        
228
        t : function () {
229
            // thanks to Matt Bannon for some much needed code-fixes here!
230
            var daysinmonths = [null,31,28,31,30,31,30,31,31,30,31,30,31];
231
            if (this.L()==1 && this.n()==2) return 29; // ~leap day
232
            return daysinmonths[this.n()];
233
        },
234
        
235
        U : function () {
236
            // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
237
            return Math.round(date.getTime()/1000);
238
        },
239
240
        w : function () {
241
            // Numeric representation of the day of the week
242
            return date.getDay();
243
        },
244
        
245
        W : function () {
246
            // Weeknumber, as per ISO specification:
247
            // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
248
        
249
            var DoW = this.N ();
250
            var DoY = this.z ();
251
252
            // If the day is 3 days before New Year's Eve and is Thursday or earlier,
253
            // it's week 1 of next year.
254
            var daysToNY = 364 + this.L () - DoY;
255
            if (daysToNY <= 2 && DoW <= (3 - daysToNY)) {
256
                return 1;
257
            }
258
259
            // If the day is within 3 days after New Year's Eve and is Friday or later,
260
            // it belongs to the old year.
261
            if (DoY <= 2 && DoW >= 5) {
262
                return new Date (this.Y () - 1, 11, 31).formatDate ("W");
263
            }
264
            
265
            var nyDoW = new Date (this.Y (), 0, 1).getDay ();
266
            nyDoW = nyDoW != 0 ? nyDoW - 1 : 6;
267
268
            if (nyDoW <= 3) { // First day of the year is a Thursday or earlier
269
                return (1 + Math.floor ((DoY + nyDoW) / 7));
270
            } else {  // First day of the year is a Friday or later
271
                return (1 + Math.floor ((DoY - (7 - nyDoW)) / 7));
272
            }
273
        },
274
        
275
        y : function () {
276
            // A two-digit representation of a year
277
            var y = String(this.Y());
278
            return y.substring(y.length-2,y.length);
279
        },        
280
        
281
        Y : function () {
282
            // A full numeric representation of a year, 4 digits
283
    
284
            // we first check, if getFullYear is supported. if it
285 8589c6df klemens
            // is, we just use that. ppks code is nice, but won't
286 a0f3aded Jonny McCullagh
            // work with dates outside 1900-2038, or something like that
287
            if (date.getFullYear) {
288
                var newDate = new Date("January 1 2001 00:00:00 +0000");
289
                var x = newDate .getFullYear();
290
                if (x == 2001) {              
291
                    // i trust the method now
292
                    return date.getFullYear();
293
                }
294
            }
295
            // else, do this:
296
            // codes thanks to ppk:
297
            // http://www.xs4all.nl/~ppk/js/introdate.html
298
            var x = date.getYear();
299
            var y = x % 100;
300
            y += (y < 38) ? 2000 : 1900;
301
            return y;
302
        },
303
304
        
305
        z : function () {
306
            // The day of the year, zero indexed! 0 through 366
307
            var s = "January 1 " + this.Y() + " 00:00:00 GMT" + this.O();
308
            var t = new Date(s);
309
            var diff = date.getTime() - t.getTime();
310
            return Math.floor(diff/1000/60/60/24);
311
        },
312
313
        Z : function () {
314
            // Timezone offset in seconds
315
            return (date.getTimezoneOffset () * -60);
316
        }        
317
    
318
    }
319
320
    function getSwitch(str) {
321
        if (switches[str] != undefined) {
322
            return switches[str]();
323
        } else {
324
            return str;
325
        }
326
    }
327
328
    var date;
329
    if (time) {
330
        var date = new Date (time);
331
    } else {
332
        var date = this;
333
    }
334
335
    var formatString = input.split("");
336
    var i = 0;
337
    while (i < formatString.length) {
338
        if (formatString[i] == "%") {
339
            // this is our way of allowing users to escape stuff
340
            formatString.splice(i,1);
341
        } else {
342
            formatString[i] = getSwitch(formatString[i]);
343
        }
344
        i++;
345
    }
346
    
347
    return formatString.join("");
348
}
349
350
351
// Some (not all) predefined format strings from PHP 5.1.1, which 
352
// offer standard date representations.
353
// See: http://www.php.net/manual/en/ref.datetime.php#datetime.constants
354
//
355
356
// Atom      "2005-08-15T15:52:01+00:00"
357
Date.DATE_ATOM    = "Y-m-d%TH:i:sP";
358
// ISO-8601  "2005-08-15T15:52:01+0000"
359
Date.DATE_ISO8601 = "Y-m-d%TH:i:sO";
360
// RFC 2822  "Mon, 15 Aug 2005 15:52:01 +0000"
361
Date.DATE_RFC2822 = "D, d M Y H:i:s O";
362
// W3C       "2005-08-15 15:52:01+00:00"
363
Date.DATE_W3C     = "Y-m-d%TH:i:sP";