2011-06-22 49 views
0

我如何在javascript中將datetime 5/8/2011 12:00:00 AM(m/d/yyyy)轉換爲dd-MMM-yyyy像08-May-2011如何在javascript中轉換dateTime格式

+0

我不認爲你會希望2011年5月8日成爲2011年6月8日。我想你的意思是'08-May-2011'? (儘管如果我看到'2011年5月8日'我讀了它作爲8月5日。模糊的日期格式是*真的很糟糕的想法) – Spudley

+0

是的它是正確的,但你知道什麼格式,所以只是提取與javascript函數和創建新的格式化日期 – Pratik

回答

0

此鏈接是一個很好的資源,你可以使用。

或者,你需要獲得各個部分,並根據需要像下面將它們連接起來。

var now = new Date(); 
var hour  = now.getHours(); 
var minute  = now.getMinutes(); 
var second  = now.getSeconds(); 
var monthnumber = now.getMonth(); 
var monthday = now.getDate(); 
var year  = now.getYear(); 

String myOutput = monthday + "-" + monthnumer + "-" + year; 

要獲取月份名稱而不是月份數字,你需要定義像數組下面

var arrMonths = new Array ("Jan","Feb"....}; 

    String myOutput = monthday + "-" + arrMonths[monthnumer-1] + "-" + year; 
+0

我有dateTime中var使用getElementById,我如何將它轉換爲日期時間 – MayureshP

+1

你應該這樣寫,var now = new Date(document.getElementById(「yourid」).value); – AjayR

相關問題