DateParse3.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. // Test non-ISO format with milliseconds
  6. // using colon as millisecond separator is not allowed
  7. runTest("2011-11-08 19:48:43:", "2011-11-08T19:48:43.000"); // valid, last colon is ignored
  8. runTest("2011-11-08 19:48:43:1", null);
  9. runTest("2011-11-08 19:48:43:10", null);
  10. runTest("2011-11-08 19:48:43:100", null);
  11. // use dot as millisecond separator
  12. runTest("2011-11-08 19:48:43.", "2011-11-08T19:48:43.000");
  13. runTest("2011-11-08 19:48:43.1", "2011-11-08T19:48:43.100");
  14. runTest("2011-11-08 19:48:43.1 ", "2011-11-08T19:48:43.100");
  15. runTest("2011-11-08 19:48:43. 1", "2011-11-08T19:48:43.100");
  16. runTest("2011-11-08 19:48:43.01", "2011-11-08T19:48:43.010");
  17. runTest("2011-11-08 19:48:43.001", "2011-11-08T19:48:43.001");
  18. runTest("2011-11-08 19:48:43.0001", "2011-11-08T19:48:43.000");
  19. runTest("2011-11-08 19:48:43.00000001", null); // having more than 7 consecutive digits causes overflow
  20. runTest("2011-11-08 19:48:43.10", "2011-11-08T19:48:43.100");
  21. runTest("2011-11-08 19:48:43.100", "2011-11-08T19:48:43.100");
  22. runTest("2011-11-08 19:48:43.1000", "2011-11-08T19:48:43.100");
  23. runTest("2011-11-08 19:48:43.12345", "2011-11-08T19:48:43.123");
  24. function runTest(dateToTest, isoDate)
  25. {
  26. if (isoDate === null) {
  27. if (isNaN(Date.parse(dateToTest))) {
  28. console.log("PASS");
  29. } else {
  30. console.log("Wrong date parsing result: Date.parse(\"" + dateToTest + "\") should return NaN");
  31. }
  32. } else {
  33. if (Date.parse(dateToTest) === Date.parse(isoDate)) {
  34. console.log("PASS");
  35. } else {
  36. console.log("Wrong date parsing result: Date.parse(\"" + dateToTest + "\") should equal Date.parse(\"" + isoDate + "\")");
  37. }
  38. }
  39. }