date.js 2.7 KB

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