Guest User

Untitled

a guest
Feb 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. var obj = {Date: new Date()};
  2. var objSer = JSON.stringify(obj);
  3. var objDeser = JSON.parse(objSer);
  4. var objJqDeser = $.parseJSON(objSer);
  5.  
  6. function getYear(value){
  7. try{
  8. return value.getYear();
  9. }
  10. catch(err){
  11. return err;
  12. }
  13. }
  14.  
  15. $("#orig").text("Orig Year: " + getYear(obj.Date));
  16. $("#deser").text("Deser Year: " + getYear(objDeser.Date));
  17. $("#jqDeser").text("JqDeser Year: " + getYear(objJqDeser.Date));
  18.  
  19. // JSON date deserializer
  20. // use as the second, 'reviver' argument to JSON.parse();
  21.  
  22. if (window.JSON && !window.JSON.dateParser) {
  23. var reISO = /^(d{4})-(d{2})-(d{2})T(d{2}):(d{2}):(d{2}(?:.d*))(?:Z|(+|-)([d|:]*))?$/;
  24. var reMsAjax = /^/Date((d|-|.*))[/|\]$/;
  25.  
  26. JSON.dateParser = function (key, value) {
  27. // first, just make sure the property is a string:
  28. if (typeof value === 'string') {
  29. // then, use regex to see if it's an ISO-formatted string
  30. var a = reISO.exec(value);
  31. if (a) {
  32. // if so, Date() can parse it:
  33. return new Date(value);
  34. }
  35. // otherwise, see if it's a wacky Microsoft-format string:
  36. a = reMsAjax.exec(value);
  37. if (a) {
  38. // and perform some jujitsu to make use of it:
  39. var b = a[1].split(/[-+,.]/);
  40. return new Date(b[0] ? +b[0] : 0 - +b[1]);
  41. }
  42. // here, you could insert any additional tests and parse instructions you like, for other date syntaxes...
  43. }
  44. // important: you need to return any values you're not parsing, or they die...
  45. return value;
  46. };
  47. }
  48.  
  49. // use: JSON.parse(json,JSON.dateParser);
  50.  
  51. var jsonDates = {
  52. dtrx2: /d{4}-d{2}-d{2}/,
  53. parse: function(obj){
  54. var parsedObj = JSON.parse(obj);
  55. return this.parseDates(parsedObj);
  56. },
  57. parseDates: function(obj){
  58. // iterate properties
  59. for(pName in obj){
  60.  
  61. // make sure the property is 'truthy'
  62. if (obj[pName]){
  63. var value = obj[pName];
  64. // determine if the property is an array
  65. if (Array.isArray(value)){
  66. for(var ii = 0; ii < value.length; ii++){
  67. this.parseDates(value[ii]);
  68. }
  69. }
  70. // determine if the property is an object
  71. else if (typeof(value) == "object"){
  72. this.parseDates(value);
  73. }
  74. // determine if the property is a string containing a date
  75. else if (typeof(value) == "string" && this.dtrx2.test(value)){
  76. // parse and replace
  77. obj[pName] = new Date(obj[pName]);
  78. }
  79. }
  80. }
  81.  
  82. return obj;
  83. }
  84. };
  85.  
  86. var curDate = new Date();
  87. document.write(curDate); //Mon Feb 01 2016 12:57:12 GMT-0600 (Central Standard Time)
  88.  
  89. var dateStr = JSON.parse(JSON.stringify(curDate));
  90. document.write(dateStr);//2016-02-01T18:59:35.375Z
  91.  
  92. var date = new Date(curDate);
  93. document.write(date); //Mon Feb 01 2016 12:59:35 GMT-0600 (Central Standard Time)
  94.  
  95. String.prototype.getYear = function() {
  96. return Date.parse(this).getYear();
  97. };
  98. var obj = {date: new Date()};
  99. var dtObj = JSON.parse(JSON.stringify(obj));
  100. console.log(dtObj.date.getYear());
  101.  
  102. var JSON_parse = JSON.parse;
  103. JSON.parse = function(str) {
  104. var res = JSON_parse(str);
  105. findAndConvertStringsToDates(res);
  106. return res;
  107. }
  108.  
  109. (function() {
  110. var jsonParse = JSON.parse;
  111. var reDate = /^d{4}-d{2}-d{2}Td{2}:d{2}:d{2}.d{3}Z$/i;
  112. function jsonDate(obj) {
  113. var type = typeof(obj);
  114. if(type == 'object') {
  115. for(var p in obj)
  116. if(obj.hasOwnProperty(p))
  117. obj[p] = jsonDate(obj[p]);
  118. return obj;
  119. } else if(type == 'string' && reDate.test(obj)) {
  120. return new Date(obj);
  121. }
  122. return obj;
  123. }
  124. JSON.parse = function(str) { return jsonDate(jsonParse(str)); }
  125. })();
  126. /*
  127. * Tests
  128. */
  129. var dt = JSON.parse(JSON.stringify({date: new Date()}));
  130. console.log(typeof(dt.date));
  131. console.log(JSON.parse(JSON.stringify(null)));
  132. console.log(JSON.parse(JSON.stringify(123)));
  133. console.log(JSON.parse(JSON.stringify("test")));
  134. console.log(JSON.parse(JSON.stringify(new Date())));
  135. console.log(JSON.parse(JSON.stringify([1,new Date(),2])));
  136. console.log(JSON.parse(JSON.stringify({d: new Date(), d2: {d3: new Date(), d4: [0,new Date(),4]}})));
Add Comment
Please, Sign In to add comment