简体   繁体   中英

how to convert javascript time object to different format?

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

this is currently displaying 2012-05-25 23:08:57 how 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);

我强烈建议您使用Date.js http://www.datejs.com/,尽管显然对于这种简单的操作,您可以很快地自己滚动。

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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM