relative.js 3.0 KB

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