date-format.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import '../_setup.js';
  2. import {parseDate, formatDate} from '../../../js/lib/date-format.js';
  3. describe('lib/date', function () {
  4. const locales = {
  5. en: {
  6. days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
  7. daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  8. daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
  9. months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  10. monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  11. today: "Today",
  12. clear: "Clear",
  13. titleFormat: "MM yyyy"
  14. },
  15. de: {
  16. days: ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"],
  17. daysShort: ["Son", "Mon", "Die", "Mit", "Don", "Fre", "Sam"],
  18. months: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
  19. monthsShort: ["Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"],
  20. },
  21. es: {
  22. days: ["Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"],
  23. daysShort: ["Dom", "Lun", "Mar", "Mié", "Jue", "Vie", "Sáb"],
  24. months: ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"],
  25. monthsShort: ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"],
  26. },
  27. fr: {
  28. days: ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"],
  29. daysShort: ["dim.", "lun.", "mar.", "mer.", "jeu.", "ven.", "sam."],
  30. daysMin: ["d", "l", "ma", "me", "j", "v", "s"],
  31. months: ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"],
  32. monthsShort: ["janv.", "févr.", "mars", "avril", "mai", "juin", "juil.", "août", "sept.", "oct.", "nov.", "déc."],
  33. }
  34. };
  35. describe('parseDate()', function () {
  36. const today = new Date().setHours(0, 0, 0, 0);
  37. const thisYear = new Date().getFullYear();
  38. it('return undefined if the given date is falsy value other than 0 or invalid date', function () {
  39. expect(parseDate(), 'to be undefined');
  40. expect(parseDate(''), 'to be undefined');
  41. expect(parseDate(new Date('')), 'to be undefined');
  42. expect(parseDate(0), 'not to be undefined');
  43. });
  44. it('returns time value of the same day\'s 00:00:00 local time if the given date is a Date object or time value', function () {
  45. const origDate = new Date().setHours(0, 0, 0, 0);
  46. expect(parseDate(new Date()), 'to be', origDate);
  47. expect(parseDate(Date.now()), 'to be', origDate);
  48. });
  49. it('invokes custom parse fucntion and returns the result if it\'s given to format.toValue', function () {
  50. const date = new Date();
  51. const format = {toValue: sinon.stub()};
  52. format.toValue.returns(date);
  53. expect(parseDate('2020-01-01', format, locales.en), 'to be', date.setHours(0, 0, 0, 0));
  54. expect(format.toValue.calledWith('2020-01-01', format, locales.en), 'to be true');
  55. });
  56. it('returns the date that the word means if given date is "today"', function () {
  57. expect(parseDate('today'), 'to be', today);
  58. });
  59. it('uses format: "d" or "dd" as day of month to parse date string', function () {
  60. expect(parseDate('2012-03-5', 'yyyy-mm-d'), 'to be', new Date(2012, 2, 5).getTime());
  61. expect(parseDate('2012-03-15', 'yyyy-mm-d'), 'to be', new Date(2012, 2, 15).getTime());
  62. expect(parseDate('2012-03-05', 'yyyy-mm-d'), 'to be', new Date(2012, 2, 5).getTime());
  63. expect(parseDate('2012-03-5', 'yyyy-mm-dd'), 'to be', new Date(2012, 2, 5).getTime());
  64. expect(parseDate('2012-03-15', 'yyyy-mm-dd'), 'to be', new Date(2012, 2, 15).getTime());
  65. expect(parseDate('2012-03-05', 'yyyy-mm-dd'), 'to be', new Date(2012, 2, 5).getTime());
  66. expect(parseDate('5/03/2012', 'dd/mm/yyyy'), 'to be', new Date(2012, 2, 5).getTime());
  67. expect(parseDate('15/03/2012', 'd/mm/yyyy'), 'to be', new Date(2012, 2, 15).getTime());
  68. });
  69. it('accepts 0 and number larger than the end of the month for "d", "dd"', function () {
  70. expect(parseDate('2012-03-0', 'yyyy-mm-d'), 'to be', new Date(2012, 1, 29).getTime());
  71. expect(parseDate('2012-03-33', 'yyyy-mm-d'), 'to be', new Date(2012, 3, 2).getTime());
  72. expect(parseDate('2013-02-60', 'yyyy-mm-d'), 'to be', new Date(2013, 3, 1).getTime());
  73. });
  74. it('uses format: "m", "mm", "M" or "MM" as month to parse date string', function () {
  75. const fakeToday = new Date(thisYear, 2, 31);
  76. const clock = sinon.useFakeTimers({now: fakeToday});
  77. // month number
  78. expect(parseDate('2012-3-5', 'yyyy-m-d'), 'to be', new Date(2012, 2, 5).getTime());
  79. expect(parseDate('2012-12-15', 'yyyy-m-d'), 'to be', new Date(2012, 11, 15).getTime());
  80. expect(parseDate('2012-03-05', 'yyyy-m-d'), 'to be', new Date(2012, 2, 5).getTime());
  81. expect(parseDate('2012-3-5', 'yyyy-mm-dd'), 'to be', new Date(2012, 2, 5).getTime());
  82. expect(parseDate('2012-12-15', 'yyyy-mm-dd'), 'to be', new Date(2012, 11, 15).getTime());
  83. expect(parseDate('2012-03-05', 'yyyy-mm-dd'), 'to be', new Date(2012, 2, 5).getTime());
  84. expect(parseDate('5/3/2012', 'dd/mm/yyyy'), 'to be', new Date(2012, 2, 5).getTime());
  85. expect(parseDate('15/12/2012', 'd/m/yyyy'), 'to be', new Date(2012, 11, 15).getTime());
  86. // ensure setting a month w/ < 31days on the 31 of a month works correctly
  87. expect(parseDate('2012-02-28', 'yyyy-mm-dd'), 'to be', new Date(2012, 1, 28).getTime());
  88. expect(parseDate('2012-09-15', 'yyyy-mm-dd'), 'to be', new Date(2012, 8, 15).getTime());
  89. // month name
  90. expect(parseDate('Mar 5, 2012', 'M d, yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
  91. expect(parseDate('Dec 15, 2012', 'M d, yyyy', locales.en), 'to be', new Date(2012, 11, 15).getTime());
  92. expect(parseDate('Mär 5, 2012', 'M d, yyyy', locales.de), 'to be', new Date(2012, 2, 5).getTime());
  93. expect(parseDate('Dez 15, 2012', 'M d, yyyy', locales.de), 'to be', new Date(2012, 11, 15).getTime());
  94. expect(parseDate('mars 5, 2012', 'M d, yyyy', locales.fr), 'to be', new Date(2012, 2, 5).getTime());
  95. expect(parseDate('déc. 15, 2012', 'M d, yyyy', locales.fr), 'to be', new Date(2012, 11, 15).getTime());
  96. expect(parseDate('March 5, 2012', 'MM d, yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
  97. expect(parseDate('December 15, 2012', 'MM d, yyyy', locales.en), 'to be', new Date(2012, 11, 15).getTime());
  98. expect(parseDate('März 5, 2012', 'MM d, yyyy', locales.de), 'to be', new Date(2012, 2, 5).getTime());
  99. expect(parseDate('Dezember 15, 2012', 'MM d, yyyy', locales.de), 'to be', new Date(2012, 11, 15).getTime());
  100. expect(parseDate('mars 5, 2012', 'MM d, yyyy', locales.fr), 'to be', new Date(2012, 2, 5).getTime());
  101. expect(parseDate('décembre 15, 2012', 'MM d, yyyy', locales.fr), 'to be', new Date(2012, 11, 15).getTime());
  102. // month number for "M" and "MM"
  103. expect(parseDate('3/5/2012', 'M/dd/yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
  104. expect(parseDate('12/15/2012', 'MM/dd/yyyy', locales.en), 'to be', new Date(2012, 11, 15).getTime());
  105. // month name for "m" and "mm"
  106. expect(parseDate('Mar/5/2012', 'm/d/yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
  107. expect(parseDate('December/15/2012', 'mm/dd/yyyy', locales.en), 'to be', new Date(2012, 11, 15).getTime());
  108. clock.restore();
  109. });
  110. it('accepts 0 and number larger than 12 for "m", "mm"', function () {
  111. const fakeToday = new Date(thisYear, 2, 31);
  112. const clock = sinon.useFakeTimers({now: fakeToday});
  113. expect(parseDate('2012-0-05', 'yyyy-m-dd'), 'to be', new Date(2011, 11, 5).getTime());
  114. expect(parseDate('2012-16-30', 'yyyy-m-d'), 'to be', new Date(2013, 3, 30).getTime());
  115. expect(parseDate('2012-32-30', 'yyyy-m-d'), 'to be', new Date(2014, 7, 30).getTime());
  116. clock.restore();
  117. });
  118. it('evaluates month name with case-insensible begin-with match', function () {
  119. const fakeToday = new Date(thisYear, 2, 31);
  120. const clock = sinon.useFakeTimers({now: fakeToday});
  121. expect(parseDate('march 5, 2012', 'M d, yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
  122. expect(parseDate('DEC 15, 2012', 'MM d, yyyy', locales.en), 'to be', new Date(2012, 11, 15).getTime());
  123. expect(parseDate('MA 5, 2012', 'MM d, yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
  124. expect(parseDate('j 5, 2012', 'MM d, yyyy', locales.en), 'to be', new Date(2012, 0, 5).getTime());
  125. expect(parseDate('ju 5, 2012', 'MM d, yyyy', locales.en), 'to be', new Date(2012, 5, 5).getTime());
  126. expect(parseDate('march/5/2012', 'm/d/yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
  127. expect(parseDate('DEC/15/2012', 'mm/dd/yyyy', locales.en), 'to be', new Date(2012, 11, 15).getTime());
  128. expect(parseDate('MA/05/2012', 'mm/dd/yyyy', locales.en), 'to be', new Date(2012, 2, 5).getTime());
  129. expect(parseDate('j/05/2012', 'mm/dd/yyyy', locales.en), 'to be', new Date(2012, 0, 5).getTime());
  130. expect(parseDate('ju/05/2012', 'mm/dd/yyyy', locales.en), 'to be', new Date(2012, 5, 5).getTime());
  131. clock.restore();
  132. });
  133. it('uses format: "y", "yy" or "yyyy" as year to parse date string', function () {
  134. expect(parseDate('2012-3-5', 'y-m-d'), 'to be', new Date(2012, 2, 5).getTime());
  135. expect(parseDate('1984-3-15', 'y-m-d'), 'to be', new Date(1984, 2, 15).getTime());
  136. expect(parseDate('12-03-05', 'y-m-d'), 'to be', new Date(0, 2, 5).setFullYear(12));
  137. expect(parseDate('2012-3-5', 'yy-m-d'), 'to be', new Date(2012, 2, 5).getTime());
  138. expect(parseDate('1984-3-15', 'yy-m-d'), 'to be', new Date(1984, 2, 15).getTime());
  139. expect(parseDate('12-03-05', 'yy-m-d'), 'to be', new Date(0, 2, 5).setFullYear(12));
  140. expect(parseDate('2012-03-5', 'yyyy-mm-dd'), 'to be', new Date(2012, 2, 5).getTime());
  141. expect(parseDate('1984-03-15', 'yyyy-mm-dd'), 'to be', new Date(1984, 2, 15).getTime());
  142. expect(parseDate('12-03-05', 'yyyy-mm-dd'), 'to be', new Date(0, 2, 5).setFullYear(12));
  143. expect(parseDate('5/03/2012', 'dd/mm/yyyy'), 'to be', new Date(2012, 2, 5).getTime());
  144. expect(parseDate('15/03/1984', 'd/m/yy'), 'to be', new Date(1984, 2, 15).getTime());
  145. });
  146. it('ignores "D" and "DD" (day of week)', function () {
  147. let date = parseDate('2012-03-05', 'yyyy-mm-dd-D');
  148. expect(date, 'to be', new Date(2012, 2, 5).getTime());
  149. date = parseDate('Sat, Dec 15, 2012', 'DD, M dd, yyyy', locales.en);
  150. expect(date, 'to be', new Date(2012, 11, 15).getTime());
  151. });
  152. it('uses current date\'s year/month/day to complement undefined, missing or unparsable parts', function () {
  153. const fakeToday = new Date(thisYear, 2, 31);
  154. const clock = sinon.useFakeTimers({now: fakeToday});
  155. expect(parseDate('03-05', 'mm-dd'), 'to be', new Date(thisYear, 2, 5).getTime());
  156. expect(parseDate('2012-06', 'yyyy-mm'), 'to be', new Date(2012, 5, 30).getTime());
  157. expect(parseDate('5, 2012', 'd, yyyy'), 'to be', new Date(2012, 2, 5).getTime());
  158. expect(parseDate('Mai/10', 'M/d', locales.en), 'to be', new Date(thisYear, 2, 10).getTime());
  159. expect(parseDate('Maya/10', 'M/d', locales.en), 'to be', new Date(thisYear, 2, 10).getTime());
  160. expect(parseDate('03-05', 'yyyy-mm-dd'), 'to be', new Date(0, 4, 31).setFullYear(3));
  161. expect(parseDate('Sun, 23', 'DD, mm-dd'), 'to be', new Date(thisYear + 1, 10, 30).getTime());
  162. expect(parseDate('Mar/05/12', 'yyyy/mm/dd', locales.en), 'to be', new Date(thisYear, 4, 12).getTime());
  163. expect(parseDate('05/Mar/12', 'yyyy/mm/dd', locales.en), 'to be', new Date(0, 2, 12).setFullYear(5));
  164. expect(parseDate('2012年十二月十五日', 'yyyy年mm月dd日', locales.en), 'to be', new Date(2012, 2, 31).getTime());
  165. clock.restore();
  166. });
  167. it('throws an Error if format is neither a valid format string nor an object w/ toValue property', function () {
  168. expect(() => parseDate('01-01-01', {}), 'to throw error');
  169. expect(() => parseDate('01-01-01', 1), 'to throw error');
  170. expect(() => parseDate('01-01-01', 'aa-bb-cc'), 'to throw error');
  171. });
  172. });
  173. describe('formatDate()', function () {
  174. it('return empty string if the given date is falsy value other than 0 or invalid date', function () {
  175. expect(formatDate(), 'to be', '');
  176. expect(formatDate(''), 'to be', '');
  177. expect(formatDate(new Date('')), 'to be', '');
  178. expect(formatDate(0, 'yyyy', locales.en), 'not to be', '');
  179. });
  180. it('invokes custom format fucntion and returns the result if it\'s given to format.toDisplay', function () {
  181. const date = new Date(2012, 2, 5);
  182. const format = {toDisplay: sinon.stub()};
  183. format.toDisplay.returns('foo-bar');
  184. expect(formatDate(date, format, locales.en), 'to be', 'foo-bar');
  185. expect(format.toDisplay.calledWith(date, format, locales.en), 'to be true');
  186. });
  187. it('uses format: "d" as day of month, no leading zero to format date', function () {
  188. expect(formatDate(new Date(2012, 2, 5), 'yyyy-mm-d', locales.en), 'to be', '2012-03-5');
  189. expect(formatDate(0, 'd', locales.en), 'to be', String(new Date(0).getDate()));
  190. });
  191. it('uses format: "dd" as day of month, leading zero to format date', function () {
  192. const date = new Date(2012, 2, 5);
  193. expect(formatDate(date, 'yyyy-mm-dd', locales.en), 'to be', '2012-03-05');
  194. expect(formatDate(date.getTime(), 'yyyy-mm-dd', locales.en), 'to be', '2012-03-05');
  195. });
  196. it('uses format: "D" as short day of week in given language to format date', function () {
  197. const date = new Date(2012, 2, 5);
  198. expect(formatDate(date, 'yyyy-mm-dd-D', locales.en), 'to be', '2012-03-05-Mon');
  199. expect(formatDate(date, 'yyyy-mm-dd-D', locales.es), 'to be', '2012-03-05-Lun');
  200. });
  201. it('uses format: "DD" as long day of week in given language to format date', function () {
  202. const date = new Date(2012, 2, 5);
  203. expect(formatDate(date, 'yyyy-mm-dd-DD', locales.en), 'to be', '2012-03-05-Monday');
  204. expect(formatDate(date, 'yyyy-mm-dd-DD', locales.es), 'to be', '2012-03-05-Lunes');
  205. });
  206. it('uses format: "m" as Month, no leading zero. to format date', function () {
  207. expect(formatDate(new Date(2012, 2, 5), 'yyyy-m-dd', locales.en), 'to be', '2012-3-05');
  208. });
  209. it('uses format: "mm" as Month, leading zero. to format date', function () {
  210. expect(formatDate(new Date(2012, 2, 5), 'yyyy-mm-dd', locales.en), 'to be', '2012-03-05');
  211. });
  212. it('uses format: "M" as month shortname in given language to format date', function () {
  213. const date = new Date(2012, 2, 5);
  214. expect(formatDate(date, 'yyyy-M-dd', locales.en), 'to be', '2012-Mar-05');
  215. expect(formatDate(date, 'yyyy-M-dd', locales.de), 'to be', '2012-Mär-05');
  216. });
  217. it('uses format: "MM" as month full name in given language to format date', function () {
  218. const date = new Date(2012, 2, 5);
  219. expect(formatDate(date, 'yyyy-MM-dd', locales.en), 'to be', '2012-March-05');
  220. expect(formatDate(date, 'yyyy-MM-dd', locales.de), 'to be', '2012-März-05');
  221. });
  222. it('uses format: "y" as Year, no leading zero. to format date', function () {
  223. expect(formatDate(new Date(0, 2, 5).setFullYear(2), 'y-m-d', locales.en), 'to be', '2-3-5');
  224. expect(formatDate(new Date(0, 2, 5).setFullYear(12), 'y-m-d', locales.en), 'to be', '12-3-5');
  225. expect(formatDate(new Date(2012, 2, 5), 'y-m-d', locales.en), 'to be', '2012-3-5');
  226. });
  227. it('uses format: "yy" as Year, two-digit. to format date', function () {
  228. expect(formatDate(new Date(0, 2, 5).setFullYear(2), 'yy-mm-dd', locales.en), 'to be', '02-03-05');
  229. expect(formatDate(new Date(0, 2, 5).setFullYear(12), 'yy-mm-dd', locales.en), 'to be', '12-03-05');
  230. expect(formatDate(new Date(2012, 2, 5), 'yy-mm-dd', locales.en), 'to be', '12-03-05');
  231. });
  232. it('uses format: "yyyy" as Year, four-digit. to format date', function () {
  233. expect(formatDate(new Date(0, 2, 5).setFullYear(2), 'yyyy-mm-dd', locales.en), 'to be', '0002-03-05');
  234. expect(formatDate(new Date(0, 2, 5).setFullYear(12), 'yyyy-mm-dd', locales.en), 'to be', '0012-03-05');
  235. expect(formatDate(new Date(2012, 2, 5), 'yyyy-mm-dd', locales.en), 'to be', '2012-03-05');
  236. });
  237. it('accepts separators come before and after the date numbers', function () {
  238. expect(formatDate(new Date(2012, 2, 5), '西暦yyyy年mm月dd日', locales.en), 'to be', '西暦2012年03月05日');
  239. });
  240. it('throws an Error if format is neither a valid format string nor an object w/ toValue property', function () {
  241. const date = new Date(2012, 2, 5);
  242. expect(() => formatDate(date, {}), 'to throw error');
  243. expect(() => formatDate(date, 1), 'to throw error');
  244. expect(() => formatDate(date, 'aa-bb-cc'), 'to throw error');
  245. });
  246. });
  247. });