0

I want to convert PHP date format to an acceptable JS format. So, for example, I want to convert M j, y g:i:s A to MMM d, y h:mm:ss a.

Ner
  • 158
  • 7

1 Answers1

1

Unfortunately, JavaScript doesn't come with a native way of string formatting (like PHP does). However, there are libraries that make up for this.

The following code prints out a PHP date inside your format and then I use date.format to translate it with JavaScript. I did modify your translating string a bit (as JavaScripts format uses different shorthand).

<script src="http://stevenlevithan.com/assets/misc/date.format.js"></script>
<script>
//documentation availiable at: http://blog.stevenlevithan.com/archives/date-time-format
var phpDate = "<?php echo $date("M j, y g:i:s A"); ?>";
var date = new Date(phpDate);
console.log(date.format("mmm d, yy h:mm:ss tt"));
</script>
Neil
  • 14,063
  • 3
  • 30
  • 51