Pages

Search This Blog

Tuesday, 7 January 2014

Convert JSON Date to the format MM/DD/YYYY

When searching for a way to convert the JSON date to format MM/DD/YYYY, I came accross the below code and this is not coded by me. Hope this post will help few who are still searching.

var newFormattedDate = ConvertJsonDateString(installDate);
$('#datepicker').val(newFormattedDate);


//function definition
function ConvertJsonDateString(jsonDate) {
             var shortDate = null;
             if (jsonDate) {
                 var regex = /-?\d+/;
                 var matches = regex.exec(jsonDate);
                 var dt = new Date(parseInt(matches[0]));
                 var month = dt.getMonth() + 1;
                 var monthString = month > 9 ? month : '0' + month;
                 var day = dt.getDate();
                 var dayString = day > 9 ? day : '0' + day;
                 var year = dt.getFullYear();
                 shortDate = monthString + '/' + dayString + '/' + year;
             }
             return shortDate;
 }  

No comments:

Post a Comment