date.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import '../_setup.js';
  2. import {
  3. stripTime,
  4. dateValue,
  5. today,
  6. addDays,
  7. addWeeks,
  8. addMonths,
  9. addYears,
  10. dayOfTheWeekOf,
  11. getWeek,
  12. startOfYearPeriod
  13. } from '../../../js/lib/date.js';
  14. describe('lib/date', function () {
  15. describe('stripTime()', function () {
  16. it('returns the time value of 00:00:00 local time of given time value or date', function () {
  17. let origDate = new Date(2016, 8, 24, 13, 55, 32, 235);
  18. expect(stripTime(origDate.getTime()), 'to be', new Date(2016, 8, 24).getTime());
  19. origDate = new Date(2019, 1, 14, 20, 55);
  20. expect(stripTime(origDate), 'to be', new Date(2019, 1, 14).getTime());
  21. });
  22. });
  23. describe('today()', function () {
  24. it('returns the time value of 00:00:00 local time of current date', function () {
  25. const now = new Date();
  26. const timeValue = today();
  27. expect(timeValue, 'to be', now.setHours(0, 0, 0, 0));
  28. });
  29. });
  30. describe('dateValue()', function () {
  31. it('returns the time value of 00:00:00 local time of the date created by given Date constructor parameters', function () {
  32. // from Date object
  33. let origDate = new Date(2016, 8, 24, 13, 55, 32, 235);
  34. let timeValue = dateValue(origDate);
  35. expect(timeValue, 'to be', new Date(2016, 8, 24).getTime());
  36. // from Date constructor args
  37. timeValue = dateValue(2019, 1, 17, 9, 43, 18, 953);
  38. expect(timeValue, 'to be', new Date(2019, 1, 17).getTime());
  39. // from string
  40. let origDateStr = '2020-04-20T02:27:15Z';
  41. origDate = new Date(origDateStr);
  42. timeValue = dateValue(origDateStr);
  43. expect(timeValue, 'to be', origDate.setHours(0, 0, 0, 0));
  44. // from time value
  45. origDate = new Date(2016, 8, 24, 13, 55, 32, 235);
  46. timeValue = dateValue(origDate.getTime());
  47. expect(timeValue, 'to be', new Date(2016, 8, 24).getTime());
  48. });
  49. it('does not map 2-digit year to 1900\'s', function () {
  50. let date = new Date(dateValue(86, 8, 24));
  51. expect(date.getFullYear(), 'to be', 86);
  52. expect(date.getMonth(), 'to be', 8);
  53. expect(date.getDate(), 'to be', 24);
  54. date = new Date(dateValue(19, 1, 17));
  55. expect(date.getFullYear(), 'to be', 19);
  56. expect(date.getMonth(), 'to be', 1);
  57. expect(date.getDate(), 'to be', 17);
  58. });
  59. });
  60. describe('addDays()', function () {
  61. it('returns the time value of N days after a date or time value', function () {
  62. let date1 = new Date(2019, 11, 31);
  63. let date2 = addDays(date1, 1);
  64. expect(date2, 'to be', new Date(2020, 0, 1).getTime());
  65. expect(addDays(date2, 30), 'to be', new Date(2020, 0, 31).getTime());
  66. date1 = new Date(2020, 0, 1);
  67. expect(addDays(date1, -1), 'to be', new Date(2019, 11, 31).getTime());
  68. expect(addDays(date1, -10), 'to be', new Date(2019, 11, 22).getTime());
  69. });
  70. });
  71. describe('addWeeks()', function () {
  72. it('returns the time value of N weeks after a date or time value', function () {
  73. let date1 = new Date(2019, 11, 28);
  74. let date2 = addWeeks(date1, 1);
  75. expect(date2, 'to be', new Date(2020, 0, 4).getTime());
  76. expect(addWeeks(date2, 4), 'to be', new Date(2020, 1, 1).getTime());
  77. date1 = new Date(2020, 0, 4);
  78. expect(addWeeks(date1, -1), 'to be', new Date(2019, 11, 28).getTime());
  79. expect(addWeeks(date1, -8), 'to be', new Date(2019, 10, 9).getTime());
  80. });
  81. });
  82. describe('addMonths()', function () {
  83. it('returns the time value of N months after a date or time value', function () {
  84. let date1 = new Date(2019, 11, 31);
  85. let date2 = addMonths(date1, 1);
  86. expect(date2, 'to be', new Date(2020, 0, 31).getTime());
  87. expect(addMonths(date2, 29), 'to be', new Date(2022, 5, 30).getTime());
  88. date1 = new Date(2020, 0, 1);
  89. expect(addMonths(date1, -1), 'to be', new Date(2019, 11, 1).getTime());
  90. expect(addMonths(date1, -18), 'to be', new Date(2018, 6, 1).getTime());
  91. });
  92. it('returns the end of the next/prev month if the day of the old date is not in the new month', function () {
  93. let date = new Date(2019, 9, 31);
  94. expect(addMonths(date, 1), 'to be', new Date(2019, 10, 30).getTime());
  95. expect(addMonths(date, 18), 'to be', new Date(2021, 3, 30).getTime());
  96. date = new Date(2019, 11, 31);
  97. expect(addMonths(date, -1), 'to be', new Date(2019, 10, 30).getTime());
  98. expect(addMonths(date, -30), 'to be', new Date(2017, 5, 30).getTime());
  99. date = new Date(2019, 0, 31);
  100. expect(addMonths(date, 1), 'to be', new Date(2019, 1, 28).getTime());
  101. expect(addMonths(date, 13), 'to be', new Date(2020, 1, 29).getTime());
  102. date = new Date(2020, 0, 31);
  103. expect(addMonths(date, 1), 'to be', new Date(2020, 1, 29).getTime());
  104. expect(addMonths(date, 13), 'to be', new Date(2021, 1, 28).getTime());
  105. date = new Date(2019, 2, 30);
  106. expect(addMonths(date, -1), 'to be', new Date(2019, 1, 28).getTime());
  107. expect(addMonths(date, -37), 'to be', new Date(2016, 1, 29).getTime());
  108. date = new Date(2020, 2, 30);
  109. expect(addMonths(date, -1), 'to be', new Date(2020, 1, 29).getTime());
  110. expect(addMonths(date, -37), 'to be', new Date(2017, 1, 28).getTime());
  111. date = new Date(2020, 1, 29);
  112. expect(addMonths(date, 12), 'to be', new Date(2021, 1, 28).getTime());
  113. expect(addMonths(date, -24), 'to be', new Date(2018, 1, 28).getTime());
  114. });
  115. });
  116. describe('addYears()', function () {
  117. it('returns the time value of N years after a date or time value', function () {
  118. let date1 = new Date(2019, 11, 31);
  119. let date2 = addYears(date1, 1);
  120. expect(date2, 'to be', new Date(2020, 11, 31).getTime());
  121. expect(addYears(date2, 3), 'to be', new Date(2023, 11, 31).getTime());
  122. date1 = new Date(2020, 0, 1);
  123. expect(addYears(date1, -1), 'to be', new Date(2019, 0, 1).getTime());
  124. expect(addYears(date1, -3), 'to be', new Date(2017, 0, 1).getTime());
  125. });
  126. it('returns 2/28 of the year if the old date is 2/29 and the new year is not a leap year', function () {
  127. const date = new Date(2020, 1, 29);
  128. expect(addYears(date, 1), 'to be', new Date(2021, 1, 28).getTime());
  129. expect(addYears(date, -1), 'to be', new Date(2019, 1, 28).getTime());
  130. expect(addYears(date, -4), 'to be', new Date(2016, 1, 29).getTime());
  131. });
  132. });
  133. describe('dayOfTheWeekOf()', function () {
  134. it('returns a date object of given day of the week of given date', function () {
  135. expect(dayOfTheWeekOf(new Date(2017, 0, 1), 0), 'to be', new Date(2017, 0, 1).getTime());
  136. expect(dayOfTheWeekOf(new Date(2017, 3, 5), 1), 'to be', new Date(2017, 3, 3).getTime());
  137. expect(dayOfTheWeekOf(new Date(2017, 5, 8), 2), 'to be', new Date(2017, 5, 6).getTime());
  138. expect(dayOfTheWeekOf(new Date(2017, 9, 31), 3), 'to be', new Date(2017, 10, 1).getTime());
  139. expect(dayOfTheWeekOf(new Date(2018, 2, 10), 4), 'to be', new Date(2018, 2, 8).getTime());
  140. expect(dayOfTheWeekOf(new Date(2018, 5, 19), 5), 'to be', new Date(2018, 5, 22).getTime());
  141. expect(dayOfTheWeekOf(new Date(2018, 10, 20), 6), 'to be', new Date(2018, 10, 24).getTime());
  142. });
  143. it('uses sepecified start day of week if it is given', function () {
  144. expect(dayOfTheWeekOf(new Date(2017, 0, 1), 0, 1), 'to be', new Date(2017, 0, 1).getTime());
  145. expect(dayOfTheWeekOf(new Date(2017, 3, 5), 1, 3), 'to be', new Date(2017, 3, 10).getTime());
  146. expect(dayOfTheWeekOf(new Date(2017, 5, 8), 2, 4), 'to be', new Date(2017, 5, 13).getTime());
  147. expect(dayOfTheWeekOf(new Date(2017, 9, 31), 3, 6), 'to be', new Date(2017, 10, 1).getTime());
  148. expect(dayOfTheWeekOf(new Date(2018, 2, 10), 4, 5), 'to be', new Date(2018, 2, 15).getTime());
  149. expect(dayOfTheWeekOf(new Date(2018, 5, 19), 5, 2), 'to be', new Date(2018, 5, 22).getTime());
  150. });
  151. });
  152. describe('getWeek()', function () {
  153. it('returns ISO week number of given date', function () {
  154. expect(getWeek(new Date(2015, 0, 1)), 'to be', 1);
  155. expect(getWeek(new Date(2015, 3, 26)), 'to be', 17);
  156. expect(getWeek(new Date(2015, 8, 12)), 'to be', 37);
  157. expect(getWeek(new Date(2015, 10, 15)), 'to be', 46);
  158. expect(getWeek(new Date(2016, 0, 1)), 'to be', 53);
  159. expect(getWeek(new Date(2016, 0, 7)), 'to be', 1);
  160. expect(getWeek(new Date(2016, 6, 21)), 'to be', 29);
  161. expect(getWeek(new Date(2016, 8, 6)), 'to be', 36);
  162. expect(getWeek(new Date(2016, 11, 1)), 'to be', 48);
  163. expect(getWeek(new Date(2017, 0, 1)), 'to be', 52);
  164. });
  165. });
  166. describe('startOfYearPeriod()', function () {
  167. it('returns the start year of given length of period of years', function () {
  168. let date = new Date(2121, 0, 1);
  169. expect(startOfYearPeriod(date, 10), 'to be', 2120);
  170. expect(startOfYearPeriod(date, 100), 'to be', 2100);
  171. expect(startOfYearPeriod(date, 1000), 'to be', 2000);
  172. date = new Date(1984, 0, 1);
  173. expect(startOfYearPeriod(date, 10), 'to be', 1980);
  174. expect(startOfYearPeriod(date, 100), 'to be', 1900);
  175. expect(startOfYearPeriod(date, 1000), 'to be', 1000);
  176. expect(startOfYearPeriod(new Date(738, 5, 30), 20), 'to be', 720);
  177. expect(startOfYearPeriod(new Date(0, 5, 30).setFullYear(88), 25), 'to be', 75);
  178. });
  179. });
  180. });