2015-08-03 45 views
0

在我的表格中日期被列爲「2015-07-31 06:02:20」。我如何使用jQuery分別獲取日期和時間?如何分別使用jQuery獲取日期和時間?

我使用了一些代碼,但它顯示了一些錯誤。

var timestamp = new Date(data.responsecusthistory.created_at); 
var date  = timestamp.toDateString(); 

錯誤:無效日期

回答

0
var date = data.responsecusthistory.created_at.split(" ")[0]; 
var time = data.responsecusthistory.created_at.split(" ")[1]; 
+0

詳細說明/解釋你的答案,它做什麼,如何以及爲什麼應該工作。 – gmo

+0

非常感謝。它工作正常 –

0

如果你想有一個時間字符串(即HH:MM:SS),例如嘗試這樣的:

var timestampTime = timestamp.toTimeString().split(' ')[0]; 

無論如何,沒有明顯的原因,你會得到「錯誤:無效的日期」,只要

data.responsecusthistory.created_at 

是正確的值,如「2015年7月31日06 :02:20" 。請考慮仔細檢查這個變量。

+0

感謝您的回覆 –

0

試試這個:

dateString =「2015-07-31 06:02:20」;

temp = new Date(dateString);

dateStr = $ .datepicker.formatDate('mm/dd/yy',temp);

獲取不同格式檢查鏈接https://github.com/phstc/jquery-dateFormat。 使用不同的格式,我們會得到不同的日期,時間等,我們需要它的任何形式。