source: Dev/trunk/src/client/dojox/date/relative.js @ 529

Last change on this file since 529 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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