-6
'Mon, 15 Jul 2013 14:27:39 -0700'

This is the format I have to work with. I'm not sure what format it is in.

However, I want to convert it to a string like this:

On Mon, 15 Jul 2013 at 2:27pm

I don't want to use basic regex or splitting spaces. I want to know the right "date" conversion way

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

1 Answers1

1

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;
});
Travis J
  • 81,153
  • 41
  • 202
  • 273