1

I used PHP code as follows to print MYSQL date value on html page. I want to do the same formatting using Jquery . How to do it?

in PHP

echo date("j F Y, g:i A",strtotime($add->added_date))

J query alert

var date = retdata[i].added_date; // date comes as 2016-02-27 06:14:15
alert (''); //formatting here??? i want as 2016 Feb 27 06:14 AM
2
  • you want to apply same format with jquery or you want to display already php formatted date with js? if second, you can inject php code into your js line. Commented Mar 20, 2016 at 8:08
  • You can send formatted date from server no need to format date twice in PHP and JavaScript; Commented Mar 20, 2016 at 9:47

2 Answers 2

1

You should use Moment.js to manipulate and parse dates in javascript.

If your date comes as a string, you should be able to parse in your format so (remember to include moment.js in your document and be careful with the format, the following is just an example and you should look for the time format of your date am,pm,24h):

var parsedDate = moment("2016-02-27 06:14:15","YYYY-MM-DD H m s");
console.log(parsedDate.format("YYYY MMM DD hh:mm A")); // 2016 Feb 27 06:14 AM
Sign up to request clarification or add additional context in comments.

1 Comment

yes! it's right answer. rather than strugling with how to convert, which manipulations to do, I prefer to use momentjs. it's more elegant and easy to use.
0

Try alert(date) since you defined date already.

You do not need to format the timestamp since you already formatted it using PHP. The only question (we cannot tell from the little bit of information you provided) is how you are passing the information to JavaScript/jQuery via the retdata array. This should show the "2016-02-27 06:14:15" (or whatever date/time information) from var date within the modal window if it is/was properly passed or defined before being called by alert().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.