Javascript 如何从javascript中的字符串获取月份?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12246394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get month from string in javascript?
提问by Edward
I have tried
我试过了
var d=new Date("2012-07-01 00:00:00.0");
alert(d.getMonth());
But getting NAN
.
但是得到NAN
.
I want month as July
for the above date.
July
以上的日期想要月。
回答by Clyde Lobo
Assuming your date is in YYYY-MM-DD format
假设您的日期是 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]);
回答by danielnixon
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"
回答by heretolearn
Try this:
尝试这个:
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
NOTE:The getMonth() returns the value in range 0-11. Therefore +1 to the value in range 1-12.
注意:getMonth() 返回 0-11 范围内的值。因此 +1 到 1-12 范围内的值。
回答by lebryant
You will need to create an array for it: http://www.w3schools.com/jsref/jsref_getmonth.asp
您需要为它创建一个数组:http: //www.w3schools.com/jsref/jsref_getmonth.asp
also why not initialise your date as: var d = new Date(2012,7,1);
还有为什么不将您的日期初始化为: var d = new Date(2012,7,1);
回答by david9
Try with this:
试试这个:
var months = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
document.write("The current month is " + months[d.getMonth()]);
January->0, February-1 and so on...
一月-> 0,二月-1 等等...
回答by Bat_Programmer
You can use this library to make things easier. http://www.datejs.com/And then try the following code:
你可以使用这个库让事情变得更简单。 http://www.datejs.com/然后试试下面的代码:
var d=new Date.parse("2012-07-01 00:00:00.0");
alert(d..tString('MMMM'));
回答by voltrevo
Short answer: with the above code most people will get an alert with 6
these days because Chrome and Firefox now understand that format. It's 6 and not 7 because Date
s understand months as indexes beginning with 0, so 0 is Jan, 1 is Feb, 2 is Mar, etc. . So the simplest fix is this:
简短的回答:使用上面的代码,现在大多数人都会收到警报,6
因为 Chrome 和 Firefox 现在可以理解这种格式。它是 6 而不是 7,因为Date
s 将月份理解为以 0 开头的索引,因此 0 是 Jan,1 是二月,2 是三月,等等。所以最简单的修复是这样的:
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]);
(Please consider using a different approach to constructing dates though!)
(请考虑使用不同的方法来构建日期!)
Long answer:
长答案:
var d=new Date("2012-07-01 00:00:00.0");
The problem is you are getting an invalid date object because the browser doesn't understand that string. Browsers aren't consistent about this - Chrome and Firefox actually parse it correctly and will alert 6
instead of NaN
. It's still not a good idea to be imprecise with your formats though - current Safari for example doesn't parse it. Formats that are guaranteed to work are described in https://tools.ietf.org/html/rfc2822#section-3.3.
问题是您得到一个无效的日期对象,因为浏览器不理解该字符串。浏览器对此并不一致 - Chrome 和 Firefox 实际上会正确解析它,并且会发出警报6
而不是NaN
. 尽管如此,对您的格式不精确仍然不是一个好主意 - 例如,当前的 Safari 不会解析它。https://tools.ietf.org/html/rfc2822#section-3.3中描述了保证有效的格式。
You might prefer to use this constructor though:
不过,您可能更喜欢使用此构造函数:
new Date(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]]);
In your example, you would call it like so:
在您的示例中,您可以这样称呼它:
var d=new Date(2012, 6, 1);
Note the 6 and not 7 though! Date
s understand months as indexes beginning with 0, so 0 is Jan, 1 is Feb, 2 is Mar, etc. . This is confusing when using the date constructor but easier to convert to July
that was asked for:
请注意 6 而不是 7!Date
s 将月份理解为以 0 开头的索引,因此 0 是 Jan,1 是 Feb,2 是 Mar,等等。这在使用日期构造函数时令人困惑,但更容易转换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]);
You can learn more about Date
here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date
您可以Date
在此处了解更多信息:https: //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date
回答by Diode
Javascript Date object doesn't store full names of month. So you have to use an array.
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()]);