Date.parse.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. test("basic functionality", () => {
  2. expect(Date.parse("2020")).toBe(1577836800000);
  3. expect(Date.parse("2000-11")).toBe(973036800000);
  4. expect(Date.parse("1980-06-30")).toBe(331171200000);
  5. expect(Date.parse("1970-06-30T13:30Z")).toBe(15600600000);
  6. expect(Date.parse("1970-01-01T00:00:59Z")).toBe(59000);
  7. expect(Date.parse("1970-01-01T00:00:00.999Z")).toBe(999);
  8. expect(Date.parse("2020T13:14+15:16")).toBe(1577829480000);
  9. expect(Date.parse("2020T13:14-15:16")).toBe(1577939400000);
  10. expect(Date.parse("2020T23:59Z")).toBe(1577923140000);
  11. expect(Date.parse("+020000")).toBe(568971820800000);
  12. expect(Date.parse("-000001")).toBe(-62198755200000);
  13. expect(Date.parse("+020000-01")).toBe(568971820800000);
  14. expect(Date.parse("+020000-01T00:00:00.000Z")).toBe(568971820800000);
  15. expect(Date.parse(2020)).toBe(1577836800000);
  16. expect(Date.parse("+1980")).toBe(NaN);
  17. expect(Date.parse("1980-")).toBe(NaN);
  18. expect(Date.parse("1980-05-")).toBe(NaN);
  19. expect(Date.parse("1980-05-00T")).toBe(NaN);
  20. expect(Date.parse("1980-05-00T15:15:")).toBe(NaN);
  21. expect(Date.parse("1980-05-00T15:15:15.")).toBe(NaN);
  22. expect(Date.parse("1980-5-30")).toBe(NaN);
  23. expect(Date.parse("1980-05-30T13")).toBe(NaN);
  24. expect(Date.parse("1980-05-30T13:4")).toBe(NaN);
  25. expect(Date.parse("1980-05-30T13:40+")).toBe(NaN);
  26. expect(Date.parse("1980-05-30T13:40+1")).toBe(NaN);
  27. expect(Date.parse("1980-05-30T13:40+1:10")).toBe(NaN);
  28. expect(Date.parse("1970-06-30T13:30Zoo")).toBe(NaN);
  29. expect(Date.parse("2020T13:30.40:")).toBe(NaN);
  30. expect(Date.parse("-000000")).toBe(NaN);
  31. });
  32. test("time clip", () => {
  33. expect(Date.parse("+999999")).toBeNaN();
  34. expect(Date.parse("-999999")).toBeNaN();
  35. });
  36. test("extra micro seconds extension", () => {
  37. expect(Date.parse("2021-04-30T15:19:02.937+00:00")).toBe(1619795942937);
  38. expect(Date.parse("2021-04-30T15:19:02.9370+00:00")).toBe(1619795942937);
  39. expect(Date.parse("2021-04-30T15:19:02.93700+00:00")).toBe(1619795942937);
  40. expect(Date.parse("2021-04-30T15:19:02.937000+00:00")).toBe(1619795942937);
  41. expect(Date.parse("2021-04-30T15:19:02.93+00:00")).toBe(1619795942930);
  42. expect(Date.parse("2021-04-30T15:19:02.9+00:00")).toBe(1619795942900);
  43. // These values are just checked against NaN since they don't have a specified timezone.
  44. expect(Date.parse("2021-04-30T15:19:02.93")).not.toBe(NaN);
  45. expect(Date.parse("2021-04-30T15:19:02.9")).not.toBe(NaN);
  46. expect(Date.parse("2021-04-30T15:19:02.+00:00")).toBe(NaN);
  47. expect(Date.parse("2021-04-30T15:19:02.")).toBe(NaN);
  48. expect(Date.parse("2021-04-30T15:19:02.a")).toBe(NaN);
  49. expect(Date.parse("2021-04-30T15:19:02.000a")).toBe(NaN);
  50. expect(Date.parse("2021-04-30T15:19:02.937001+00:00")).toBe(1619795942937);
  51. expect(Date.parse("2021-04-30T15:19:02.937999+00:00")).toBe(1619795942937);
  52. expect(Date.parse("2021-06-26T07:24:40.007000+00:00")).toBe(1624692280007);
  53. expect(Date.parse("2021-06-26T07:24:40.0079999999999999999+00:00")).toBe(1624692280007);
  54. expect(Date.parse("2021-04-15T18:47:25.606000+00:00")).toBe(1618512445606);
  55. });
  56. test("extra date extension", () => {
  57. function expectStringToGiveDate(input, fullYear, month, dayInMonth) {
  58. // Since the timezone is not specified we just say it has to equal the date parts.
  59. const date = new Date(Date.parse(input));
  60. expect(date.getFullYear()).toBe(fullYear);
  61. expect(date.getMonth() + 1).toBe(month);
  62. expect(date.getDate()).toBe(dayInMonth);
  63. }
  64. expectStringToGiveDate("01/30/2021", 2021, 1, 30);
  65. expectStringToGiveDate("10/1/2021", 2021, 10, 1);
  66. expectStringToGiveDate("7/07/1977", 1977, 7, 7);
  67. expectStringToGiveDate("2/27/3058", 3058, 2, 27);
  68. });
  69. test("mm/dd/yy hh:mm timezone-offset extension", () => {
  70. // Examples from Discord's JavaScript for Christmas 2022.
  71. expect(Date.parse("12/05/2022 10:00 -0800")).toBe(1670263200000);
  72. expect(Date.parse("01/03/2023 10:00 -0800")).toBe(1672768800000);
  73. });
  74. test("yy{/,-}mm{/,-}dd hh:mm extension", () => {
  75. function expectStringToGiveDate(input, fullYear, month, dayInMonth, hours, minutes) {
  76. // Since the timezone is not specified we just say it has to equal the date parts.
  77. const date = new Date(Date.parse(input));
  78. expect(date.getFullYear()).toBe(fullYear);
  79. expect(date.getMonth() + 1).toBe(month);
  80. expect(date.getDate()).toBe(dayInMonth);
  81. expect(date.getHours()).toBe(hours);
  82. expect(date.getMinutes()).toBe(minutes);
  83. }
  84. // Example from a UK news website.
  85. expectStringToGiveDate("2014/11/14 13:05", 2014, 11, 14, 13, 5);
  86. expectStringToGiveDate("2014-11-14 13:05", 2014, 11, 14, 13, 5);
  87. });
  88. test("Month dd, yy hh:mm:ss extension", () => {
  89. function expectStringToGiveDate(input, fullYear, month, dayInMonth, hours, minutes, seconds) {
  90. // Since the timezone is not specified we just say it has to equal the date parts.
  91. const date = new Date(Date.parse(input));
  92. expect(date.getFullYear()).toBe(fullYear);
  93. expect(date.getMonth() + 1).toBe(month);
  94. expect(date.getDate()).toBe(dayInMonth);
  95. expect(date.getHours()).toBe(hours);
  96. expect(date.getMinutes()).toBe(minutes);
  97. expect(date.getSeconds()).toBe(seconds);
  98. }
  99. // Examples from Discord's Birthday JavaScript for May 2023.
  100. expectStringToGiveDate("May 15, 2023 17:00:00", 2023, 5, 15, 17, 0, 0);
  101. expectStringToGiveDate("May 22, 2023 17:00:00", 2023, 5, 22, 17, 0, 0);
  102. expectStringToGiveDate("May 30, 2023 17:00:00", 2023, 5, 30, 17, 0, 0);
  103. expectStringToGiveDate("June 5, 2023 17:00:00", 2023, 6, 5, 17, 0, 0);
  104. });