Definition and Usage
The setUTCMonth() method is used to set the month based on Universal Time (UTC).
Syntax
dateObject.setUTCMonth(month,day)
Parameters | Description |
month | Required. The value of the month field to be set for dateObject, expressed in universal time. This parameter is an integer between 0 (January) ~ 11 (December). |
day | Optional. An integer between 1 and 31, used as the day field of dateObject, expressed in universal time. |
Return value
Adjusted date expressed in milliseconds.
Tips and Notes:
Note: This method is always used in conjunction with a Date object.
Tip: For more information about Universal Coordinated Time (UTC), please refer to Baidu Encyclopedia.
Example
Example 1
In this example, we will set the month field to 0 (January) through the setUTCMonth() method:
<script type="text/javascript"> var d=new Date() d.setUTCMonth(0) document.write(d) </script>
Output:
Sat Jan 07 2017 14:17:55 GMT+0800 (中国标准时间)
Example 2
In this example, we will set the month to 0 (January) and the day field to 20 through setUTCMonth():
<script type="text/javascript"> var d=new Date() d.setUTCMonth(0,20) document.write(d) </script>
Output:
Fri Jan 20 2017 14:17:55 GMT+0800 (中国标准时间)
All parameters of the setUTCMonth() function can exceed the normal value range. For example: the parameter month can exceed the conventional value range of 0 ~ 11; the parameter dateValue can exceed the conventional value range of 1 ~ 31; and both can be negative numbers. The Date object is automatically calculated and converted to the corresponding date internally.
Note that the value of parameter month is 1 less than the actual month.
Return value
The setUTCMonth() function has no return value (or in other words, the return value is undefined).
Example & Description
// 当前运行环境的时区为 UTC +8 //定义一个本地时间的Date对象"2013-05-15 00:00:00" // 对应的UTC时间为"2013-05-14 16:00:00" var date = new Date(2013, 4, 15, 0, 0, 0); document.writeln( date.toLocaleString() ); // 2013年5月15日 0:00:00 date.setUTCMonth(0); document.writeln( date.toLocaleString() ); // 2013年1月15日 0:00:00 // 此时的UTC时间为"2013-01-14 16:00:00" date.setUTCMonth(-15, 2); // 设置完毕后,UTC为"2013-(-14)-(02) 16:00:00" 即"2011-10-02 16:00:00" // 输出本地时间即为"2011-10-03 00:00:00" document.writeln( date.toLocaleString() ); // 2011年10月3日 0:00:00 date.setUTCMonth(12, 5); document.writeln( date.toLocaleString() ); // 2012年1月6日 0:00:00
The above is the detailed content of JavaScript method setUTCMonth() to set the month according to Universal Time (UTC). For more information, please follow other related articles on the PHP Chinese website!