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
.
Asked
Active
Viewed 1,272 times
1 Answers
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