TwoDigitYears.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // A two digit year in the format mm/dd/yy is interpreted as a 21st century year if it is less than 50;
  6. // otherwise it's interpreted as a 20th century year.
  7. // See: https://github.com/Microsoft/ChakraCore/pull/4062
  8. // 21st century years:
  9. runTest("01/01/00", "2000-01-01T00:00:00");
  10. runTest("01/01/00 00:00:01 am", "2000-01-01T00:00:01");
  11. runTest("01/01/00 00:00:01 am EST", "2000-01-01T00:00:01-05:00");
  12. runTest("11/14/17", "2017-11-14T00:00:00");
  13. runTest("10/26/49", "2049-10-26T00:00:00");
  14. runTest("12/31/49 11:59:59 pm", "2049-12-31T23:59:59");
  15. runTest("12/31/49 11:59:59 pm PST", "2049-12-31T23:59:59-08:00");
  16. // 20st century years:
  17. runTest("01/01/50", "1950-01-01T00:00:00");
  18. runTest("01/01/50 01:34:59", "1950-01-01T01:34:59");
  19. runTest("09/27/70", "1970-09-27T00:00:00");
  20. runTest("12/31/99", "1999-12-31T00:00:00");
  21. runTest("12/31/99 11:59:59 p.m.", "1999-12-31T23:59:59");
  22. runTest("12/31/99 11:59:59 p.m. UTC", "1999-12-31T23:59:59Z");
  23. function runTest(dateToTest, isoDate) {
  24. if (isoDate === null) {
  25. if (isNaN(Date.parse(dateToTest))) {
  26. console.log("PASS");
  27. } else {
  28. console.log("Wrong date parsing result: Date.parse(\"" + dateToTest + "\") should return NaN");
  29. }
  30. } else {
  31. if (Date.parse(dateToTest) === Date.parse(isoDate)) {
  32. console.log("PASS");
  33. } else {
  34. console.log("Wrong date parsing result: Date.parse(\"" + dateToTest + "\") should equal Date.parse(\"" + isoDate + "\")");
  35. }
  36. }
  37. }