我努力了
var d=new Date("2012-07-01 00:00:00.0");
alert(d.getMonth());
但是越来越NAN
。
July
上述日期想要月份。
我努力了
var d=new Date("2012-07-01 00:00:00.0");
alert(d.getMonth());
但是越来越NAN
。
July
上述日期想要月份。
假设您的日期是 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" ];
var month_index = parseInt(arr[1],10) - 1;
console.log("The current month is " + months[month_index]);
Using 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"
尝试这个:
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
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(); //get the value of month
console.log(monthNames[m]) // Print the month name
注意: getMonth() 返回 0-11 范围内的值。
另一种选择是使用toLocaleString
var dateObj = new Date("2012-07-01");
//To get the long name for month
var monthName = dateObj.toLocaleString("default", { month: "long" });
// monthName = "November"
//To get the short name for month
var monthName = dateObj.toLocaleString("default", { month: "short" });
// monthName = "Nov"
Javascript Date 对象不存储月份的全名。所以你必须使用一个数组。
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()]);
您需要为其创建一个数组:http ://www.w3schools.com/jsref/jsref_getmonth.asp
还有为什么不将您的日期初始化为: var d = new Date(2012,7,1);
试试这个:
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 等等......
You can use this library to make things easier. http://www.datejs.com/ And then try the following code:
var d=new Date.parse("2012-07-01 00:00:00.0");
alert(d..tString('MMMM'));
简短的回答:使用上面的代码,这些天大多数人都会收到警报,6
因为 Chrome 和 Firefox 现在可以理解这种格式。它是 6 而不是 7,因为Date
s 将月份理解为以 0 开头的索引,因此 0 是 Jan,1 是 Feb,2 是 Mar,等等。所以最简单的解决方法是:
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://www.rfc-editor.org/rfc/rfc2822#section-3.3中描述了保证有效的格式。
不过,您可能更喜欢使用此构造函数:
new Date(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]]);
在您的示例中,您可以这样称呼它:
var d=new Date(2012, 6, 1);
请注意 6 而不是 7!Date
s 将月份理解为以 0 开头的索引,因此 0 是一月,1 是二月,2 是三月,等等。这在使用日期构造函数时会令人困惑,但更容易转换为July
所要求的:
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
:https ://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date