source: Dev/branches/rest-dojo-ui/client/dojox/dtl/utils/date.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 2.7 KB
Line 
1define([
2        "dojo/_base/lang",
3        "dojox/date/php",
4        "../_base"
5], function(lang,ddp,dd){
6        /*=====
7                ddp = dojox.data.php;
8                dd = dojox.dtl;
9        =====*/
10        lang.getObject("dojox.dtl.utils.date", true);
11
12        dd.utils.date.DateFormat = ddp.DateFormat;
13        lang.extend(dd.utils.date.DateFormat, ddp.DateFormat.prototype, {
14                f: function(){
15                        // summary:
16                        //              Time, in 12-hour hours and minutes, with minutes left off if they're zero.
17                        // description:
18                        //              Examples: '1', '1:30', '2:05', '2'
19                        //              Proprietary extension.
20                        return (!this.date.getMinutes()) ? this.g() : this.g() + ":" + this.i();
21                },
22                N: function(){
23                        // summary: Month abbreviation in Associated Press style. Proprietary extension.
24                        return dojox.dtl.utils.date._months_ap[this.date.getMonth()];
25                },
26                P: function(){
27                        // summary:
28                        //              Time, in 12-hour hours, minutes and 'a.m.'/'p.m.', with minutes left off
29                        //              if they're zero and the strings 'midnight' and 'noon' if appropriate.
30                        // description:
31                        //              Examples: '1 a.m.', '1:30 p.m.', 'midnight', 'noon', '12:30 p.m.'
32                        //              Proprietary extension.
33                        if(!this.date.getMinutes() && !this.date.getHours()){
34                                return 'midnight';
35                        }
36                        if(!this.date.getMinutes() && this.date.getHours() == 12){
37                                return 'noon';
38                        }
39                        return this.f() + " " + this.a();
40                }
41        });
42
43        lang.mixin(dojox.dtl.utils.date, {
44                format: function(/*Date*/ date, /*String*/ format){
45                        var df = new dojox.dtl.utils.date.DateFormat(format);
46                        return df.format(date);
47                },
48                timesince: function(d, now){
49                        // summary:
50                        //              Takes two datetime objects and returns the time between then and now
51                        //              as a nicely formatted string, e.g "10 minutes"
52                        // description:
53                        //              Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
54                        if(!(d instanceof Date)){
55                                d = new Date(d.year, d.month, d.day);
56                        }
57                        if(!now){
58                                now = new Date();
59                        }
60
61                        var delta = Math.abs(now.getTime() - d.getTime());
62                        for(var i = 0, chunk; chunk = dojox.dtl.utils.date._chunks[i]; i++){
63                                var count = Math.floor(delta / chunk[0]);
64                                if(count) break;
65                        }
66                        return count + " " + chunk[1](count);
67                },
68                _chunks: [
69                        [60 * 60 * 24 * 365 * 1000, function(n){ return (n == 1) ? 'year' : 'years'; }],
70                        [60 * 60 * 24 * 30 * 1000, function(n){ return (n == 1) ? 'month' : 'months'; }],
71                        [60 * 60 * 24 * 7 * 1000, function(n){ return (n == 1) ? 'week' : 'weeks'; }],
72                        [60 * 60 * 24 * 1000, function(n){ return (n == 1) ? 'day' : 'days'; }],
73                        [60 * 60 * 1000, function(n){ return (n == 1) ? 'hour' : 'hours'; }],
74                        [60 * 1000, function(n){ return (n == 1) ? 'minute' : 'minutes'; }]
75                ],
76                _months_ap: ["Jan.", "Feb.", "March", "April", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."]
77        });
78        return dojox.dtl.utils.date;
79});
Note: See TracBrowser for help on using the repository browser.