relative.js 3.4 KB

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