JavaScript中的Date.getMonth()函数
Date对象是JavaScript语言中内置的数据类型。日期对象使用新的Date()创建,如下所示。
创建Date对象后,可以使用多种方法对其进行操作。大多数方法仅允许您使用本地时间或UTC(通用或GMT)时间来获取和设置对象的年,月,日,时,分,秒和毫秒字段。
getMonth()Date对象的功能返回其当前日期的蛾(0代表一月,以此类推)。
语法
其语法如下
dateObj.getMonth();
示例
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var dateObj = new Date('september 26, 89 12:4:25:96');
document.write("Month of the year: "+dateObj.getMonth());
</script>
</body>
</html>输出结果
Month of the year: 8
示例
如果创建日期对象时未提到一年中的月份,则此函数返回0。
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var dateObj = new Date('89 12:4:25:96');
document.write("Month of the year: "+dateObj.getmonth());
</script>
</body>
</html>输出结果
Month of the year: 0
示例
以同样的方式,如果您在创建日期对象时未传递任何内容,则此函数将返回当前年份的当前月份。
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var dateObj = new Date();
document.write("Month of the year:+ "dateObj.getMonth());
</script>
</body>
</html>输出结果
Month of the year: 9