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