2012-09-03 28 views
3

我試圖如何從JavaScript中的字符串獲取月份?

 var d=new Date("2012-07-01 00:00:00.0"); 
    alert(d.getMonth()); 

但要NAN

對於上述日期,我希望月份爲July

+1

該字符串不解析... –

+0

試試這個string =「2012-07-01 00:00:00」。可能是最後一個'.0'導致了問題 – PoeHaH

+1

請參閱http://stackoverflow.com/questions/476105/how-can-i-convert-string-to-datetime-with-format-specification-in- javascript如果你想解析的格式 –

回答

10

假設你的日期是YYYY-MM-DD格式

var arr = "2012-07-01 00:00:00.0".split("-"); 
var months = [ "January", "February", "March", "April", "May", "June", 
    "July", "August", "September", "October", "November", "December" ]; 
console.log("The current month is " + months[parseInt(arr[1],10)]) 
0

嘗試這樣的:

var months = [ "January", "February", "March", "April", "May", "June", 
    "July", "August", "September", "October", "November", "December" ]; 
document.write("The current month is " + months[d.getMonth()]); 

一月至> 0,二月-1等等...

3

試試這個:

var str="2012-07-01"; //Set the string in the proper format(best to use ISO format ie YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS) 
    var d=new Date(str); //converts the string into date object 
    var m=d.getMonth()+1; //get the value of month 
    alert(m);   // Display the month value 

注意: getMonth()返回範圍0-11中的值。因此+1範圍1-12中的值。

+1

他想要幾個月的名字 – Kageetai

0

你可以使用這個庫來簡化操作。 http://www.datejs.com/ 然後再試試下面的代碼:

var d=new Date.parse("2012-07-01 00:00:00.0"); 
alert(d..tString('MMMM')); 
0

javascript日期對象不存儲月份的全名。所以你必須使用一個數組。

var dateString = "2012-07-01 00:00:00.0"; 

var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; 

var date = new Date(dateString.replace(" ", "T")); 
alert(monthNames [date.getMonth()]); 
3

使用the JavaScript Internationalization API

var date = new Date("2012-07-01"); 

var monthName = new Intl.DateTimeFormat("en-US", { month: "long" }).format; 
var longName = monthName(date); // "July" 

var shortMonthName = new Intl.DateTimeFormat("en-US", { month: "short" }).format; 
var shortName = shortMonthName(date); // "Jul" 
+0

似乎每個現代瀏覽器都支持http://caniuse.com/#search=internationalization –

0

簡短的回答:與上面的代碼大部分人會得到一個警報,6這些天,因爲Chrome和Firefox現在明白了格式。它是6而不是7,因爲Date將月份理解爲從0開始的索引,所以0是Jan,1是2月,2是3月等。因此最簡單的解決方法是這樣的:

var d=new Date("2012-07-01 00:00:00.0"); 
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; 
var monthIndex = d.getMonth(); 
alert(months[monthIndex]); 

(請考慮使用不同的方法,雖然構造日期!)

龍答:

var d=new Date("2012-07-01 00:00:00.0"); 

問題是您購買的無效日期因爲瀏覽器不理解該字符串。瀏覽器對此並不一致 - Chrome和Firefox實際上會正確解析它,並會提醒6而不是NaN。儘管你的格式不準確,但現在的Safari並不解析它。保證工作的格式在https://tools.ietf.org/html/rfc2822#section-3.3中描述。

你可能更願意雖然使用此構造:

new Date(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]]); 

在你的榜樣,你會叫它像這樣:

var d=new Date(2012, 6, 1); 

注6和7未不過! Date將月份理解爲以0開始的索引,所以0是Jan,1是2月,2是3月等。

var d=new Date(2012, 6, 1); 
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; 
var monthIndex = d.getMonth(); 
alert(months[monthIndex]); 

您可以瞭解更多關於Date這裏:使用日期構造函數,但更容易轉化爲July這是詢問時,這是令人困惑https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date

相關問題