您好,欢迎访问一九零五行业门户网

JavaScript根据世界时 (UTC) 来设置月份的方法setUTCMonth()

定义和用法
setutcmonth() 方法用于根据世界时 (utc) 来设置月份。
语法
dateobject.setutcmonth(month,day)
参数 描述
month 必需。要给 dateobject 设置的月份字段的值,用世界时表示。
该参数是 0(一月) ~ 11(十二月) 之间的整数。
day 可选。在 1 ~ 31 之间的整数,用作 dateobject 的天字段,用世界时表示。
返回值
调整过的日期的毫秒表示。
提示和注释:
注释:该方法总是结合一个 date 对象来使用。
提示:有关通用协调时间 (utc) 的更多资料,请参阅百度百科。
实例
例子 1
在本例中,我们将通过 setutcmonth() 方法把月字段设置为 0 (一月):
<script type="text/javascript"> var d=new date() d.setutcmonth(0) document.write(d) </script>
输出:
sat jan 07 2017 14:17:55 gmt+0800 (中国标准时间)
例子 2
在本例中,我们将通过 setutcmonth() 把月份设置为 0 (一月),把天字段设置为 20:
<script type="text/javascript"> var d=new date() d.setutcmonth(0,20) document.write(d) </script>
输出:
fri jan 20 2017 14:17:55 gmt+0800 (中国标准时间)
setutcmonth()函数的所有参数都可以超出常规取值范围。例如:参数month可以超出常规的0 ~ 11的取值范围;参数datevalue可以超出常规的1 ~ 31的取值范围;并且都可以为负数。date对象内部会自动计算并转换为相应的日期。
注意,参数month的值比实际月份小1。
返回值
setutcmonth()函数没有返回值(或者说,返回值为undefined)。
示例&说明
// 当前运行环境的时区为 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
以上就是javascript根据世界时 (utc) 来设置月份的方法setutcmonth()的详细内容。
其它类似信息

推荐信息