简体   繁体   中英

Format Javascript Array into JSON by converting all dates into ISOString format

I have javascript array of objects with key and value in it. I have so many dates in the array and I want to convert the entire array into proper JSON and in the process I also convert all dates inside array into ISOString format. I can only use JQuery, UnderscoreJS or momentz libraries.

Initial Format of my javascript array:

{  
   "primaryPerformerId":"122418",
   "primaryGroupingId":"63913",
   "primaryCategoryId":"1",
   "name":"Test Concert Event",
   "venueId":"82",
   "placeConfigs":[  
      {  
         "placeConfigId":"1232392"
      }
   ],
   "defaultLocale":"en_US",
   "metas":[  
      {  
         "templateId":"201",
         "name":"Test Concert Event",
         "locale":"en_US"
      }
   ],
   "unknownEventDateIndicator":"false",
   "unknownEventTimeIndicator":"false",
   "eventStartTime":"05/18/2016 08:04 PM",
   "trueOnSaleDate":"05/18/2016 08:04 PM",
   "firstPresaleDate":null,
   "status":"active",
   "dynamicAttributes":[  

   ],
   "lastChanceDate":"05/18/2016 08:04 PM",
   "onSaleDate":"05/15/2016 08:04 PM",
   "confirmDate":"05/16/2016 08:04 PM",
   "earliestPossibleInhandDate":"05/16/2016 08:04 PM",
   "latestPossibleInhandDate":"05/18/2016 08:04 PM"
}

Expected format:

{  
   "primaryPerformerId":"122418",
   "primaryGroupingId":"63913",
   "primaryCategoryId":"1",
   "name":"Test Concert Event",
   "venueId":"82",
   "placeConfigs":[  
      {  
         "placeConfigId":"1232392"
      }
   ],
   "defaultLocale":"en_US",
   "metas":[  
      {  
         "templateId":"201",
         "name":"Test Concert Event",
         "locale":"en_US"
      }
   ],
   "unknownEventDateIndicator":"false",
   "unknownEventTimeIndicator":"false",
   "eventStartTime":"2016-05-18T20:04:00.000Z",
   "trueOnSaleDate":"2016-05-17T20:03:00.000Z",
   "firstPresaleDate":null,
   "status":"active",
   "dynamicAttributes":[  

   ],
   "lastChanceDate":"2016-05-18T20:04:00.000Z",
   "onSaleDate":"2016-05-12T23:38:18.775Z",
   "confirmDate":"2016-05-11T23:38:18.775Z",
   "earliestPossibleInhandDate":"2016-05-10T20:04:00.000Z",
   "latestPossibleInhandDate":"2016-05-11T20:04:00.000Z"
}

This should do. It uses regex to find the date values and JSON.stringify with a custom handler to put it all together. I would also like to point out that this will calculate the timezone as being whatever system timezone that this script is running on. As the output time will be in UTC you might want to make sure the timezone is correct beforehand.

 var o = { "primaryPerformerId":"122418", "primaryGroupingId":"63913", "primaryCategoryId":"1", "name":"Test Concert Event", "venueId":"82", "placeConfigs":[ { "placeConfigId":"1232392" } ], "defaultLocale":"en_US", "metas":[ { "templateId":"201", "name":"Test Concert Event", "locale":"en_US" } ], "unknownEventDateIndicator":"false", "unknownEventTimeIndicator":"false", "eventStartTime":"05/18/2016 08:04 PM", "trueOnSaleDate":"05/18/2016 08:04 PM", "firstPresaleDate":null, "status":"active", "dynamicAttributes":[ ], "lastChanceDate":"05/18/2016 08:04 PM", "onSaleDate":"05/15/2016 08:04 PM", "confirmDate":"05/16/2016 08:04 PM", "earliestPossibleInhandDate":"05/16/2016 08:04 PM", "latestPossibleInhandDate":"05/18/2016 20:04" }; document.body.innerText = JSON.stringify(o, function(key, value) { var res; if(res = /^\\s*([0-9]{1,2})\\s*\\/\\s*([0-9]{1,2})\\s*\\/\\s*([0-9]{1,4})\\s+([0-9]{1,2})\\s*\\:\\s*([0-9]{1,2})(?:\\s*(AM|PM))?\\s*$/i.exec(value)) { value = (o.defaultLocale === 'en_US' ? new Date(res[3], res[1]-1, res[2], res[6] ? (res[6].toUpperCase() === 'PM' ? 12 : 0) + (res[4] === '12' ? 0 : parseInt(res[4])) : res[4], res[5]) : new Date(res[3], res[2]-1, res[1], res[6] ? (res[6].toUpperCase() === 'PM' ? 12 : 0) + (res[4] === '12' ? 0 : parseInt(res[4])) : res[4], res[5]) ).toISOString(); } return value; }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM