1 | define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/date/locale", "dojo/i18n"], function(dojo, dlang, ddl, i18n){ |
---|
2 | |
---|
3 | dojo.getObject("date.relative", true, dojox); |
---|
4 | |
---|
5 | /*===== |
---|
6 | dojox.date.relative.__FormatOptions = function(){ |
---|
7 | // locale: String |
---|
8 | // override the locale used to determine formatting rules |
---|
9 | // relativeDate: Date |
---|
10 | // Date to calculate relation to (defaults to new Date()) |
---|
11 | // weekCheck: boolean |
---|
12 | // Whether or not to display the day of week (defaults true) |
---|
13 | this.locale = locale; |
---|
14 | this.relativeDate = relativeDate; |
---|
15 | this.weekCheck = weekCheck; |
---|
16 | } |
---|
17 | =====*/ |
---|
18 | |
---|
19 | var DAY = 1000*60*60*24, |
---|
20 | SIX_DAYS = 6 * DAY, |
---|
21 | del = dojo.delegate, |
---|
22 | ggb = ddl._getGregorianBundle, |
---|
23 | fmt = ddl.format; |
---|
24 | |
---|
25 | function _clearTime(date){ |
---|
26 | date = new Date(date); |
---|
27 | date.setHours(0, 0, 0, 0); |
---|
28 | return date; |
---|
29 | } |
---|
30 | |
---|
31 | dojox.date.relative.format = function(/*Date*/dateObject, /*dojox.date.relative.__FormatOptions?*/options){ |
---|
32 | // summary: |
---|
33 | // Format a Date object as a String, using locale-specific settings, |
---|
34 | // relative to the current date or some other date. |
---|
35 | // |
---|
36 | // description: |
---|
37 | // Create a string from a Date object using the most significant information |
---|
38 | // and a known localized pattern. This method formats both the date and |
---|
39 | // time from dateObject. Formatting patterns are chosen appropriate to |
---|
40 | // the locale. |
---|
41 | // |
---|
42 | // If the day portion of the date falls within the current date (or the |
---|
43 | // relativeDate option, if present), then the time will be all that |
---|
44 | // is displayed |
---|
45 | // |
---|
46 | // If the day portion of the date falls within the past week (or the |
---|
47 | // week preceeding relativeDate, if present), then the display will show |
---|
48 | // day of week and time. This functionality can be turned off by setting |
---|
49 | // weekCheck to false. |
---|
50 | // |
---|
51 | // If the year portion of the date falls within the current year (or the |
---|
52 | // year portion of relativeDate, if present), then the display will show |
---|
53 | // month and day. |
---|
54 | // |
---|
55 | // Otherwise, this function is equivalent to calling dojo.date.format with |
---|
56 | // formatLength of "medium" |
---|
57 | // |
---|
58 | // dateObject: |
---|
59 | // the date and time to be formatted. |
---|
60 | |
---|
61 | options = options || {}; |
---|
62 | |
---|
63 | var today = _clearTime(options.relativeDate || new Date()), |
---|
64 | diff = today.getTime() - _clearTime(dateObject).getTime(), |
---|
65 | fmtOpts = {locale: options.locale}; |
---|
66 | |
---|
67 | if(diff === 0){ |
---|
68 | // today: 9:32 AM |
---|
69 | return fmt(dateObject, del(fmtOpts, {selector: "time"})); |
---|
70 | }else if(diff <= SIX_DAYS && diff > 0 && options.weekCheck !== false){ |
---|
71 | // within the last week: Mon 9:32 am |
---|
72 | return fmt(dateObject, del(fmtOpts, {selector: "date", datePattern: "EEE"})) + |
---|
73 | " " + |
---|
74 | fmt(dateObject, del(fmtOpts, {selector: "time", formatLength: "short"})); |
---|
75 | }else if(dateObject.getFullYear() == today.getFullYear()){ |
---|
76 | // this year: Nov 1 |
---|
77 | var bundle = ggb(i18n.normalizeLocale(options.locale)); |
---|
78 | return fmt(dateObject, del(fmtOpts, { |
---|
79 | selector: "date", |
---|
80 | datePattern: bundle["dateFormatItem-MMMd"] |
---|
81 | })); |
---|
82 | }else{ |
---|
83 | // default: Jun 1, 2010 |
---|
84 | return fmt(dateObject, del(fmtOpts, { |
---|
85 | selector: "date", |
---|
86 | formatLength: "medium", |
---|
87 | locale: options.locale |
---|
88 | })); |
---|
89 | } |
---|
90 | }; |
---|
91 | }); |
---|