DateGetSet9.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. var d = new Date();
  6. d.setDate(12345678);
  7. d.setTime(456789);
  8. WScript.Echo("toISOString : " + d.toISOString());
  9. WScript.Echo("toJSON : " + d.toJSON());
  10. // Test NaN Date value
  11. d = new Date(Number.NaN);
  12. try
  13. {
  14. d.toISOString();
  15. } catch(e) {
  16. WScript.Echo("NaN Date toISOString: " + e.name + " : " + e.message);
  17. }
  18. WScript.Echo("NaN Date toJSON:: " + d.toJSON());
  19. //
  20. // Test Infinity Date value
  21. //
  22. d = new Date(Infinity);
  23. try {
  24. d.toISOString();
  25. } catch(e) {
  26. WScript.Echo("Infinity Date toISOString : " + e.name + " : " + e.message);
  27. }
  28. WScript.Echo("Infinity Date toJSON : " + d.toJSON());
  29. //
  30. // Test Date.prototype.toJSON transferred to an object but toISOString is not callable
  31. //
  32. d = {
  33. toISOString: 1,
  34. toJSON: Date.prototype.toJSON
  35. };
  36. try {
  37. d.toJSON();
  38. } catch(e) {
  39. WScript.Echo("Object toISOString not callable : " + e.name + " : " + e.message);
  40. }
  41. //
  42. // Test Date.prototype.toJSON transferred to an object
  43. //
  44. d = {
  45. toISOString: function() {
  46. return "Fake JSON : Object";
  47. },
  48. toJSON: Date.prototype.toJSON
  49. };
  50. WScript.Echo("Object toJSON : " + d.toJSON());
  51. //
  52. // Test Date.prototype.toJSON transferred to String
  53. //
  54. String.prototype.toISOString = function() {
  55. return "Fake JSON : " + this;
  56. };
  57. String.prototype.toJSON = Date.prototype.toJSON;
  58. d = "String";
  59. WScript.Echo("String toJSON : " + d.toJSON());
  60. //
  61. // Test Date.getYear -- ES5 spec B.2.4
  62. //
  63. WScript.Echo("getYear 2000: " + new Date("January 1 2000").getYear());
  64. WScript.Echo("getYear 1899: " + new Date("January 1 1899").getYear());
  65. Object.defineProperty(Date.prototype, "valueOf", {get: function() {WScript.Echo("get fired");}});
  66. var d = new Date();
  67. d.toJSON();