[Solved] Convert date string object to another format [duplicate]


jsFiddle Demo

This looks like a situation where you are going to have to do some custom formatting based on knowing the exact format of the input. You should probably take a sliding window approach using substring with a hardcoded knowledge of the format.

(I used jQuery here for brevity in the demo. Note that the actual formatting is pure javascript.)

var t = $("#date").text();
$("#sol").html(function(){
 var result = "On ";
 var sub = t.substr(0,17);
 t = t.substr(17);
 result += sub + "at ";
 var pre = t.substr(0,2);
 t = t.substr(2,3);
 var suffix = "am";
 if( parseInt(pre) > 11 ){
   suffix = "pm";
   if( pre != 12 ){
     pre = parseInt(pre) - 12;   
   }
 }
 result += pre + t + suffix;
 return result;
});

solved Convert date string object to another format [duplicate]