Furqan Furqan - 5 months ago 20
jQuery Question

How can convert YYYY-MM-DD hh mm ss into MonthName, date, year | Hour:Minuts (am/pm) in JavaScript or jQuery

I have date string

(2013-03-10 19:43:55)
and want to convert it in this format
(Mar 10, 2013 | 7:43 pm)
using JavaScript or jQuery. How can I do this ??

Answer

Extending @arun-p-johny solution for a cross-browser compatible solution

function converter(s) {

  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  s =  s.replace(/-/g, '/');
  var d = new Date(s);

  var hour = d.getHours();

  return d.getFullYear() + ' ' + months[d.getMonth()] + ' ' + d.getDate() + ' | ' + (hour % 12) + ':' + d.getMinutes() + ' ' + (hour > 11 ? 'pm' : 'am');
}

console.log(converter('2013-03-10 19:43:55'));

Reference: http://dygraphs.com/date-formats.html

Comments