定义和用法
getmonth() 方法可返回表示月份的数字。
语法
dateObject.getMonth()
返回值
dateObject 的月份字段,使用本地时间。返回值是 0(一月) 到 11(十二月) 之间的一个整数。
提示和注释:
注释:该方法总是结合一个 Date 对象来使用。
实例
例子 1
在本例中,我们将取得当前的日期,并把它输出:
输出:
立即学习“Java免费学习笔记(深入)”;
10
例子 2
现在,我们将创建一个数组,以输出月份的名称,而不是一个数字:
输出:
立即学习“Java免费学习笔记(深入)”;
The month is November
getMonth()函数的返回值为Number类型,返回当前Date对象的月份值。该值介于 [0, 11] 之间。
其中,0 ~ 11 分别表示 1月至12月。
示例&说明
// 定义一个当前时间的Date对象(2014-08-07) var date = new Date(); document.writeln( date.getMonth() ); // 7 // 定义一个"2002-06-30 12:11:59 230"的Date对象 var date2 = new Date(2002, 5, 30, 12, 11, 59, 230); document.writeln( date2.getMonth() ); // 5 // 定义一个"2009-01-18"的Date对象 var date3 = new Date(2009, 0, 18); document.writeln( date3.getMonth() ); // 0











