0

I have a javascript object of date and its value is: Thu Dec 18 2014 07:29:44 GMT+0500 (PKT)

Now I want to get the time from above data object in following format:

7:29 A.M

How can I do that using javascript code.

Please Help!!

Muhammad Raza
  • 424
  • 5
  • 22
  • possible duplicate of [How do you display javascript datetime in 12 hour AM/PM format?](http://stackoverflow.com/questions/8888491/how-do-you-display-javascript-datetime-in-12-hour-am-pm-format) – rfj001 Dec 18 '14 at 07:34

1 Answers1

1

You can do a little string addition by doing the following:

var result = "";
var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();

var amORpm = hours > 12 ? "P.M" : "A.M";
if(hours > 12) hours -= 12;
result = hours + ":" + minutes + " " + amORpm;
console.log(result); // Will get you your desired format
Alex J
  • 1,029
  • 6
  • 8