2013-01-24 28 views
-2

大家转换的JavaScript生成日期字符串不同的字符串中的JS或PHP

我的问题是,我的JS代码格式生成日期

“星期四2013年1月10日00:00:00 GMT + 0200(FLE标准时间)”

,我需要将其转换像格式化这个

‘2013年1月10日’

无论是在JS或PHP,因为我存储数据库中的日期这种格式。我的第一个想法是将其转换为时间戳,然后将时间戳转换为新的日期字符串,但日期或字符串函数无法读取该类型的日期格式,所以需要pregreplace一些事情,多数民众赞成它。

+0

你可以发表一些代码请 – user1909426

+1

你试过什么吗?什么都可以? –

+3

http://codepad.viper-7.com/jrujDx – Leri

回答

0

下面是一个例子,如何可以在JavaScript格式:日日

function formatDate(date) { 
    var year = date.getFullYear(), 
     month = date.getMonth() + 1, 
     day = date.getDate(); 
    if (month.toString().length === 1) { 
     month = '0' + month; 
    } 
    if (day.toString().length === 1) { 
     day = '0' + day; 
    } 
    return year + '-' + month + '-' + day; 
} 
formatDate(new Date("Thu Jan 10 2013 00:00:00 GMT+0200 (FLE Standard Time)")); 
//"2013-01-10" 
+1

非常好,正是我需要的,谢谢你,先生! – user1909823

+0

我很高兴我的答案解决了您的问题:-) –

1

阅读关于PHP strtotime

<?php 

$date_str = "Thu Jan 10 2013 00:00:00 GMT+0200"; 

echo date('Y-m-d',strtotime($date_str));
0

尝试使用date.js的转换。

var myDate = new Date("Thu Jan 10 2013 00:00:00 GMT+0200 (FLE Standard Time)").toString('yyyy-MM-dd'); 
console.log(myDate); //2013-01-10 
0

您可以使用此JavaScript代码来获取日期。

<script type="text/javascript"> 
    var currentTime = new Date() 
    var month = currentTime.getMonth() + 1 
    var day = currentTime.getDate() 
    var year = currentTime.getFullYear() 
    document.write("" +year + "-" + month + "-" + day + "") 
</script> 
0

你可以做类似的东西:

var date = new Date("Thu Jan 10 2013 00:00:00 GMT+0200 (FLE Standard Time)"), 
    month, 
    day, 
    date_string; 

date_string = date.getFullYear() + '-' + 
    ((month = date.getMonth() + 1) < 10 ? '0' + month : month) + '-' + 
    ((day = date.getDate()) < 10 ? '0' + day : day); 

DATE_STRING现在持有的 “2013年1月10日” 的值。