date.js 2.9 KB

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