0

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"
}
6
  • JSON's stringify is recursive so you don't need to prep the dates. that said, dates aren't actually a JSON type, so you need to revive() them on parse() Commented May 12, 2016 at 23:52
  • I cannot understand your answer. I have javascript array in the said intial format, I want to convert it into JSON and send it to server, in this convertion process I need to convert all the dates inside array into ISOString format before sending it to server, the initial format is entered by user in mm/dd/yyyy or dd/mm/yyyy format based on the locale. Commented May 13, 2016 at 17:20
  • Right, so, loop and replace them. Commented May 13, 2016 at 17:40
  • Is there anything wrong with my answer? Commented May 13, 2016 at 18:35
  • when you encode the whole array, Dates become ISO strings automatically, no looping or conversion needed. Commented May 14, 2016 at 22:55

1 Answer 1

1

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;
    });

Sign up to request clarification or add additional context in comments.

6 Comments

Someone seems to be downvoting answers literally seconds after they are posted. Mine was downvoted exactly 3 seconds after posting. I compared the timestamps... Probably someone that is extremely grumpy.
I've updated the regex in my answer to be much more forgiving with the date values it parses.
Your answer seems to be helpful for me. The time in date will be 12 or 24 format sometimes. For all non US it is 24 hour format.
I also prefer either JQuery or momentz, if it can be done more efficiently with more simpler code.
@user1614862 jQuery is only for DOM. I can get it to work with moment if you really want. However with my updated regex I would advise against that as the updated regex will probably be even more forgiving than moment would. I could do it without regex using moment but that would attempt to parse everything indiscriminately which would be inefficient. I will definitely update my answer to include 24 hour support though.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.