date.setUTCHours()是JavaScript中的内置函数,用于根据使用Date()构造函数创建的通用时间将小时设置为日期对象。
用法:
DateObj.setUTCHours(hours_Value);
DateObj是使用Date()构造函数创建的有效Date对象,我们要在其中设置小时数。
参数:这里的参数hours_Value是小时值,用于设置使用date()构造函数创建的日期对象。
返回值:它返回由setUTCHours()函数设置的新的即更新的小时。
以下示例程序旨在说明setUTCHours()函数:
<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 hour 11 is being set in above Date
// Object with the help of setUTCHours() function
dateobj.setUTCHours(11);
// new hour from above Date Object is
// being extracted using getUTCHours()
var B = dateobj.getUTCHours();
// Printing new hour
document.write(B);
</script>
输出:
11
错误和异常
- 示例1:如果在Date()构造函数中我们在创建Date对象时没有给出小时,则setUTCHours()函数仍将能够根据通用时间设置新小时,这是在创建的Date对象中作为其参数给出的。
<script> // Here hour has not been assigned according to // universal time while creating Date object var dateobj = new Date('October 13, 1996 GMT-3:00'); // new hour 11 is being set in above Date // Object with the help of setUTCHours() function dateobj.setUTCHours(11); // new hour from above Date Object is // being extracted using getUTCHours() var B = dateobj.getUTCHours(); // Printing new hour document.write(B); </script>
输出:
11
- 示例2:如果在Date()构造函数中未提供任何参数作为参数,则setUTCHours()函数仍将能够设置小时,但根据通用时间,月,年和日期仍为当前时间。
<script> // Here nothing has been assigned // while creating Date object var dateobj = new Date(); // new hour 11 is being set in above Date // Object with the help of setUTCHours() function dateobj.setUTCHours(11); // hour from above Date Object is // being extracted using getUTCHours() var B = dateobj.getUTCHours(); // month from above Date Object is // being extracted using getUTCMonth() var C = dateobj.getUTCMonth(); // date from above Date Object is // being extracted using getUTCDate() var D = dateobj.getUTCDate(); // year from above Date Object is // being extracted using getUTCFullYear() var E = dateobj.getUTCFullYear(); // Printing new Hour document.write(B + "<br>"); // Printing current month document.write(C + "<br>"); // Printing current date document.write(D + "<br>"); // Printing current year document.write(E); </script>
输出:
11 2 30 2018
这里11是新小时,2是当前月,即3月,30是当前日期,2018是当前年。
- 示例3:如果将小时值设置为26作为setHours()函数的参数,则它将2设置为小时,因为小时范围是从0到23的形式,因此,这里减去24是因为0到23是24。
<script> // Here nothing has been assigned // while creating Date object var dateobj = new Date('October 13, 1996 05:35:32'); // new hour 26 is being set in above Date // Object with the help of setUTCHours() function dateobj.setUTCHours(26); // hour from above Date Object is // being extracted using getUTCHours() var B = dateobj.getUTCHours(); // month from above Date Object is // being extracted using getUTCMonth() var C = dateobj.getUTCMonth(); // date from above Date Object is // being extracted using getUTCDate() var D = dateobj.getUTCDate(); // year from above Date Object is // being extracted using getUTCFullYear() var E = dateobj.getUTCFullYear(); // Printing new Hour document.write(B + "<br>"); // Printing month document.write(C + "<br>"); // Printing date document.write(D + "<br>"); // Printing year document.write(E); </script>
输出:
2 9 14 1996
支持的浏览器:JavaScript date.setUTCHours()函数支持的浏览器如下:
- 谷歌浏览器
- IE浏览器
- 火狐浏览器
- Opera
- 苹果浏览器
相关用法
- Javascript Math.pow( )用法及代码示例
- Javascript Array some()用法及代码示例
- Javascript Number()用法及代码示例
- Javascript Symbol.for()用法及代码示例
- Javascript toExponential()用法及代码示例
- Javascript toString()用法及代码示例
- Javascript Math.abs( )用法及代码示例
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 JavaScript | date.setUTCHours() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。