2013-02-06 17 views
1

如何更改Jquery/Javascript中的日期格式?我已經下載了 date.js。於是,我按照下面的例子:如何在JQuery/Javascript中更改日期格式?

var d1 = Date.parse('2010-10-18, 10:06 AM'); 
alert(d1.toString('dd/mm/yyyy HH:mm:ss GMT')); 

所以,我寫我自己的下列日期代碼:

Thu Jan 31 2013 16:29:30 GMT+0700 (WIT)

var d1 = Date.parse("Thu Jan 31 2013 16:29:30 GMT+0700 (WIT)"); 
alert(d1.toString('dd/mm/yyyy')); 

我logcat的打印這樣的:

CordovaLog(31455): Uncaught RangeError: toString() radix argument must be between 2 and 36

我使用的PhoneGap爲我的android應用程序。

+0

使用時刻,而不是嘗試。這對於日期解析和格式化非常有用http://momentjs.com/我認爲momentjs是最接近我們在javascript中使用JodaTime的。另一種選擇是使用[jquery datepicker](http://docs.jquery.com/UI/Datepicker/formatDate)的日期格式,但我認爲momentjs更好。 – fmsf

+0

我對momentjs真的很感興趣,但文檔很長,我找不到任何方法將時間戳轉換爲格式化的日期。 – Rendy

+0

時刻(TimeStampInMillis).format('DD/MM/YYYY') – fmsf

回答

-1

您可以使用JavaScript來設置日期和時間的格式。 http://www.webdevelopersnotes.com/tips/html/10_ways_to_format_time_and_date_using_javascript.php3

function formatDate(date) { 
    var d1 = Date.parse(date); 
    var d = new Date(d1); 
    var curr_date = d.getDate(); 
    if (curr_date.toString().length == 1) { 
     curr_date = "0" + curr_date; 
    } 
    var curr_month = d.getMonth(); 
    curr_month++; 
    if (curr_month.toString().length == 1) { 
     curr_month = "0" + curr_month; 
    } 
    var curr_year = d.getFullYear(); 
    console.log(curr_date + "/" + curr_month + "/" + curr_year); 
} 

formatDate("Thu Jan 31 2013 16:29:30 GMT+0700 (WIT)"); 
//Returns the date in "dd/mm/yyyy" format 
0

試試這個..

var d = new Date(); 
var newDate = d.getDate()+'/'+(d.getMonth()+1)+'/'+d.getFullYear(); 

alert('newDate '+newDate);