There was an issue with the date format coming from the server, displaying "Thu Jan 07 2016 11:00:40 GMT+0530 (IST)". I managed to convert this date into a more readable format as "1/7/2016 11:00 am". However, my goal is to have the date displayed in mm/dd/yyyy format along with the time in AM and PM 12-hour format. While Chrome browser correctly displays the desired format, when building the project on an iPad, it shows "Nan" instead of the date and time.
Please assist me with resolving this issue.
Below is the code for formatting the date:
This function takes a date object and converts it into DD/MM/YYYY format with a 12-hour clock time.
function formatDate(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + strTime;
}
The following code snippet extracts the date from JSON data and converts it into the specified format.
var date = new Date(data.Steps.records[i].CreatedDate);
var convertedDate = formatDate(date);
console.log("New date format: " + convertedDate);
data.Steps.records[i].CreatedDate = convertedDate;
console.log("Original date: " + date);