date.setUTCMonth()是JavaScript中的内置函数,用于根据通用时间将月份设置为使用date()构造函数创建的日期对象。
用法:
dateObj.setUTCMonth(month_Value);
DateObj是使用Date()构造函数创建的有效Date对象,我们要在其中构造月份。月份的值从0到11,因为一年中从一月到十二月的月份总数是12。值0用于一月,1用于二月,依此类推,直到12月11。
参数:这里的参数是month_Value,即用于在date()构造函数中设置的month的值。
返回值:它返回由setUTCMonth()函数设置的新的即更新的月份。
<script>
// Here a date has been assigned according to
// universal time while creating Date object
var dateobj = new Date('October 13, 1996 05:35:32 GMT-3:00');
// new month of January is being set in above Date
// Object with the help of setUTCMonth() function
dateobj.setUTCMonth(0);
// new month from above Date Object is
// being extracted using getUTCMonth()
var B = dateobj.getMonth();
// Printing new month
document.write(B);
</script>
输出:
0
错误和异常
- 示例1:如果在Date()构造函数中没有给出任何月份,则setUTCMonth()函数仍设置新的月份作为其参数。
<script> // Here month has not been assigned according to // universal time while creating Date object var dateobj = new Date('1996, 05:35:32 GMT-3:00'); // new month of 2 is being set in above Date // Object with the help of setUTCMonth() function dateobj.setUTCMonth(2); // new month from above Date Object is // being extracted using getUTCMonth() var B = dateobj.getUTCMonth(); // Printing new month document.write(B); </script>
输出:
2
- 示例2:如果Date()构造函数中未提供任何参数作为参数,则setUTCMonth()函数仍将设置月份,但年,日期等将根据通用时间变为当前时间。
<script> // Here nothing has been assigned according to // universal time while creating Date object var dateobj = new Date(); // new month of 11 is being set in above Date // Object with the help of setUTCMonth() function dateobj.setUTCMonth(11); // Month from above Date Object is // being extracted using getUTCMonth() var B = dateobj.getUTCMonth(); // date from above Date Object is // being extracted using getUTCDate() var C = dateobj.getUTCDate(); // year from above Date Object is // being extracted using getUTCFullYear() var D = dateobj.getUTCFullYear(); // Printing new month document.write(B + '<br>'); // Printing current date document.write(C + '<br>'); // Printing current year document.write(D + '<br>'); </script>
输出:
11 1 2018
根据通用时间,此处11是12月的新月,1是当前日期,而2018是当前年份。
- 示例3:如果将月15的值作为setUTCMonth()函数的参数给出,则会将3设置为月,因为月范围是0到11,因此,这里减去12是因为0到11为12。
<script> // Here date has been assigned according to // universal time while creating Date object var dateobj = new Date('October 13, 1996 05:35:32 GMT-3:00'); // new month of 15 is being set in above Date // Object with the help of setUTCMonth() function dateobj.setUTCMonth(15); // month from above Date Object is // being extracted using getUTCMonth() var B = dateobj.getUTCMonth(); // year from above Date Object is // being extracted using getUTCFullYear() var C = dateobj.getUTCFullYear(); // Printing new month document.write(B + '<br>'); // Printing new year document.write(C); </script>
输出:
3 1997
这里3是4月的新月,因为月份范围是从0到11,即总数为12,所以从1996年成为1997年,我们将新月份设置为3,从1996年到1997年增加一年,月份根据通用时间变成3 。
支持的浏览器:JavaScript date.setUTCMonth()函数支持的浏览器如下:
- 谷歌浏览器
- IE浏览器
- 火狐浏览器
- Opera
- 苹果浏览器
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 JavaScript | date.setUTCMonth()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。