I recently received a JSON response that includes dates that need to be formatted. Here is an example of the data:
{
"webservice_status": {
"status": "SUCCESS",
"message": ""
},
"informationList": [{
"TestNumber": "12",
"Color": "RED",
"dateOfPaint": "10242016",
"location": "Wall"
}, {
"TestNumber": "13",
"Color": "BLUE",
"dateOfPaint": "10232016",
"location": "Floor"
}, {
"TestNumber": "14",
"Color": "GREEN",
"dateOfPaint": "1052016",
"location": "Wall"
}, {
"TestNumber": "15",
"Color": "BLACK",
"dateOfPaint": "10232016",
"location": "Wall"
}]
}
After attempting to use Moment.js to format the dates, I encountered an issue where the date was returning as Invalid Date
. Here is the code snippet with the problem:
this.processJSON = function (data_, textStatus_, jqXHR_){
var dateMoment = data_.informationList[0].dateOfPaint;
console.log("The date we are testing:" + dateMoment);
console.log(moment(dateMoment).format('MM/DD/YYYY'));
}
Given this situation, I am considering if it's possible to manually add slashes to the date string such as 10242016
, transforming it into 10/24/2016
, without using Moment.js. However, I would need to account for cases where the dates already have slashes and are in the correct format within the JSON response.