Date.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. Ext.define('Ext.calendar.util.Date', {
  2. singleton: true,
  3. diffDays: function(start, end) {
  4. var day = 1000 * 60 * 60 * 24,
  5. clear = Ext.Date.clearTime,
  6. diff = clear(end, true).getTime() - clear(start, true).getTime();
  7. return Math.ceil(diff / day);
  8. },
  9. copyTime: function(fromDt, toDt) {
  10. var dt = Ext.Date.clone(toDt);
  11. dt.setHours(
  12. fromDt.getHours(),
  13. fromDt.getMinutes(),
  14. fromDt.getSeconds(),
  15. fromDt.getMilliseconds());
  16. return dt;
  17. },
  18. compare: function(dt1, dt2, precise) {
  19. if (precise !== true) {
  20. dt1 = Ext.Date.clone(dt1);
  21. dt1.setMilliseconds(0);
  22. dt2 = Ext.Date.clone(dt2);
  23. dt2.setMilliseconds(0);
  24. }
  25. return dt2.getTime() - dt1.getTime();
  26. },
  27. // private helper fn
  28. maxOrMin: function(max) {
  29. var dt = (max ? 0: Number.MAX_VALUE),
  30. i = 0,
  31. args = arguments[1],
  32. ln = args.length;
  33. for (; i < ln; i++) {
  34. dt = Math[max ? 'max': 'min'](dt, args[i].getTime());
  35. }
  36. return new Date(dt);
  37. },
  38. max: function() {
  39. return this.maxOrMin.apply(this, [true, arguments]);
  40. },
  41. min: function() {
  42. return this.maxOrMin.apply(this, [false, arguments]);
  43. },
  44. today: function() {
  45. return Ext.Date.clearTime(new Date());
  46. },
  47. /**
  48. * Adds time to the specified date and returns a new Date instance as the result (does not
  49. * alter the original date object). Time can be specified in any combination of milliseconds
  50. * to years, and the function automatically takes leap years and daylight savings into account.
  51. * Some syntax examples:<code><pre>
  52. var now = new Date();
  53. // Add 24 hours to the current date/time:
  54. var tomorrow = Extensible.Date.add(now, { days: 1 });
  55. // More complex, returning a date only with no time value:
  56. var futureDate = Extensible.Date.add(now, {
  57. weeks: 1,
  58. days: 5,
  59. minutes: 30,
  60. clearTime: true
  61. });
  62. </pre></code>
  63. * @param {Date} dt The starting date to which to add time
  64. * @param {Object} o A config object that can contain one or more of the following
  65. * properties, each with an integer value:
  66. *
  67. * - millis
  68. * - seconds
  69. * - minutes
  70. * - hours
  71. * - days
  72. * - weeks
  73. * - months
  74. * - years
  75. *
  76. * You can also optionally include the property "clearTime: true" which will perform all of the
  77. * date addition first, then clear the time value of the final date before returning it.
  78. * @return {Date} A new date instance containing the resulting date/time value
  79. */
  80. add : function(dt, o) {
  81. if (!o) {
  82. return dt;
  83. }
  84. var ExtDate = Ext.Date,
  85. dateAdd = ExtDate.add,
  86. newDt = ExtDate.clone(dt);
  87. if (o.years) {
  88. newDt = dateAdd(newDt, ExtDate.YEAR, o.years);
  89. }
  90. if (o.months) {
  91. newDt = dateAdd(newDt, ExtDate.MONTH, o.months);
  92. }
  93. if (o.weeks) {
  94. o.days = (o.days || 0) + (o.weeks * 7);
  95. }
  96. if (o.days) {
  97. newDt = dateAdd(newDt, ExtDate.DAY, o.days);
  98. }
  99. if (o.hours) {
  100. newDt = dateAdd(newDt, ExtDate.HOUR, o.hours);
  101. }
  102. if (o.minutes) {
  103. newDt = dateAdd(newDt, ExtDate.MINUTE, o.minutes);
  104. }
  105. if (o.seconds) {
  106. newDt = dateAdd(newDt, ExtDate.SECOND, o.seconds);
  107. }
  108. if (o.millis) {
  109. newDt = dateAdd(newDt, ExtDate.MILLI, o.millis);
  110. }
  111. return o.clearTime ? ExtDate.clearTime(newDt) : newDt;
  112. }
  113. });