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.

0

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.