2013-12-17 58 views
0

我想在格式Dec 24, 2013這是格式化newDate目前喜歡 Tue Dec 24 2013 00:00:00 GMT+0530格式日期 「M d,YY」

var dateString = 'Dec 17, 2013'; // date string 
var actualDate = new Date(dateString); // convert to actual date 
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+7); 

alert(dateString); 

alert(newDate); 

小提琴:http://jsfiddle.net/7eRXh/1/

+0

我想你的意思是'2013年12月24日',對不對? – h2ooooooo

+0

http://blog.stevenlevithan.com/archives/date-time-format – Vishal

+0

@ h2ooooooo是的,我該怎麼做? – Neo

回答

1

希望這可以幫助你。進一步參見本document

$.datepicker.formatDate('M d, yy', 
new Date(), 
{ monthNamesShort: ['Jan','Feb','Mar','Apr','Maj','Jun','Jul', 
        'Aug','Sep','Okt','Nov','Dec'] 
}); 
0

在純Javascript

沒有爲在Javascript默認日期沒有三位數格式。因此,創建一個像

var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", 
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; 

陣列一旦你的是日期進行解析,通過newDate值如下

var con = monthNames[newDate.getMonth()] + " " + newDate.getDate() + ", " + 
               newDate.getFullYear(); 
//returns Dec 24, 2013 

JSFiddle

0

您可以使用date.js

有一個名爲toString方法,它接受一些format specifiers

代碼:

var dateString = 'Dec 17, 2013'; // date string 
var actualDate = new Date(dateString); // convert to actual date 
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+7); 
var newDate2 = newDate.toString("MMM d, yyyy"); 

alert(newDate2); 

演示:http://jsfiddle.net/MWQAE/

0

其實你不需要任何額外的js庫。只需原生Javascript就足夠了。 嘗試

//Extend prototype so you can call it in any Date instance 
Date.prototype.customFormat = function() { 
    var months = ['Jan','Feb','Mar','Apr','Maj','Jun','Jul','Aug','Sep','Okt','Nov','Dec'],t = this; 
    return [months[t.getMonth() % 12], " ", t.getDate(), ", ", t.getFullYear()].join("") 
} 

//Now 
alert((new Date).customFormat()); 
//7 days after 
alert((new Date(+new Date + 7*24*3600*1000)).customFormat()); 
相關問題