0
    tableHTML += "<td class=\"stamp\">"+jsonObject[i]['stamp']+"</td>";

this is currently displaying 2012-05-25 23:08:57how can i change this to a May 25,2012

a commentor noted that it may be better to do this with php, the only time i could do it with php is before entering it to the array. is this possible?

$videos_array = array();
while ($i < $num) {
$current_video = array (
'stamp'=>mysql_result($result,$i,"stamp"));
array_push($videos_array, $current_video);
        $i++;
    }
    echo json_encode($videos_array);
3
  • Is this something you are receiving from a php page? it may be better to do this server side Commented May 26, 2012 at 21:27
  • if i did it with php, i would need to convert it prior to entering it into the array? i edited my question above, if you know how to convert it with php, prior to entering it into the array, let me know. Commented May 26, 2012 at 21:29
  • I'm not sure that formatting dates in PHP is the right way to go. It is best to think of dates as data. It is true in JSON that you have to have to have rendered in some format, and ISO 8601 is pretty much the world's new standard. So if you are getting the date string 2012-05-25 23:08:57 from PHP, wait until the last second to format it in JavaScript. You code will be easier to internationalize, assuming you want to display dates in other locales.... Commented May 26, 2012 at 21:50

2 Answers 2

1

I would strongly recommend using Date.js http://www.datejs.com/ though obviously for something this simple you could roll your own pretty quickly.

Sign up to request clarification or add additional context in comments.

Comments

0

Use a real date library as Dr.Dredel suggests, but if you want an awful hack, there is always

new Date(Date.parse("2012-05-25 23:08:57")).toDateString().substring(4)

Not to be used in serious code. It relies on the output of toDateString which is locale dependent. The proper way to do this is to take a date object and just before you are to display it, choose a date format.

The built-in date object in JavaScript does not do a good job of this.

You can find examples of its use in the MDN documentation.

ADDENDUM

Okay, I've gotten serious and produced an example using datejs. You can find a live working version here.

Basically, put the following in your HTML:

<script src="http://cachedcommons.org/cache/datejs/1.0.0/javascripts/date-min.js"></script>

​ Then in your JavaScript adapt the following code:

var d = Date.parse("2012-05-25 23:08:57");
alert(d.toString("MMM dd, yyyy"));

2 Comments

ok i downloaded the real date library, but i have not idea how to use it. i just include it on my page, and then what?
I've updated the answer to show a usage of datejs. It is a very nice library, even though it doesn't appear to have been updated in years. I'm not too familiar with other libraries.

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.