標籤:blog http strong io for art cti div
這個在php+mssql(日期類型為datetime)+ajax的時候才能用到,js需要把時間戳記轉為為普通格式,一般的情況下可能用不到
[php] view plaincopy
- <script>
- function getLocalTime(nS) {
- return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,‘ ‘);
- }
- alert(getLocalTime(1293072805));
- </script>
彈出:2010年12月23日 10:53
也可以用:
[php] view plaincopy
- <script>
- function getLocalTime(nS) {
- return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)}
- alert(getLocalTime(1293072805));
- </script>
如果想彈出:2010-10-20 10:00:00這個格式的也好辦
[php] view plaincopy
- <script>
- function getLocalTime(nS) {
- return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
- }
- alert(getLocalTime(1177824835));
- </script>
另外我也參考了別的網站的一些東東,如下:
[php] view plaincopy
- function formatDate(now) {
- var year=now.getYear();
- var month=now.getMonth()+1;
- var date=now.getDate();
- var hour=now.getHours();
- var minute=now.getMinutes();
- var second=now.getSeconds();
- return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
- }
-
- var d=new Date(1230999938);
- alert(formatDate(d));
特別要提一下的是:
從MySQL傳過來的數據,有可能是字串,要把他們轉化為數字,數字也要*1000,因為JS裡用的是毫秒數!我的如下:
[php] view plaincopy
- var time_num = $("date",message).text();
- var time_num = parseInt(time_num); //傳回來的是個字串
- var d = new Date(time_num*1000); //這個很重要,要*1000
- var temp_time = formatDate(d);
第一個例子做的就比較正規,什麽情況都考慮到了!