下面是一个例子 Date getUTCHours() 方法
null
- 例子:
javascript
<script> // Here a date has been assigned according // to universal time while creating Date object var dateobj = new Date( 'October 15, 1996 23:35:32 GMT+11:00' ); // hour from above date object is being // extracted using getUTCHours(). var B = dateobj.getUTCHours(); // Printing hour according to universal time. document.write(B); </script> |
- 输出:
12
这个 日期getUTCHours() 方法用于根据通用时间从给定的日期对象获取小时数。 语法:
DateObj.getUTCHours();
参数: 此方法不接受任何参数。它只是和一个日期对象一起使用,我们想根据世界时间从中获取小时数。 返回值: 它根据世界时间返回给定日期对象的小时数。小时是一个介于0到23之间的整数值。 注: 这个 DateObj 是一个使用Date()constructor创建的有效日期对象,我们希望根据世界时从中获取小时数。 以上方法的更多代码如下: 项目1: 该月的日期应该在1到31之间,因为没有一个月的日期大于31,这就是它返回NaN的原因,也就是说,不是一个数字,因为该月的日期不存在。如果该月的日期不存在,根据世界时,小时将不存在。
javascript
<script> // Here a date has been assigned according // to universal time while creating Date object var dateobj = new Date( 'October 33, 1996 23:35:32 GMT+11:00' ); // Hour from above date object is // being extracted using getUTCHours(). var B = dateobj.getUTCHours(); // Printing hour according to universal time. document.write(B); </script> |
输出:
NaN
项目2: 如果在创建日期对象时没有给出小时数,getUTCHours()方法将返回零(0),但输出将打印为13,因为根据世界时,0表示为13。这是个例外。
javascript
<script> // Here a date has been assigned according // to universal time while creating Date object var dateobj = new Date( 'October 13, 1996 GMT+11:00' ); // Hour from above date object is // being extracted using getUTCHours(). var B = dateobj.getUTCHours(); // Printing hour according to universal time. document.write(B); </script> |
输出:
13
方案3: 如果在创建Date对象时没有向Date()构造函数提供任何参数,getUTCHours()方法将根据通用时间返回当前小时。
javascript
<script> // creating Date Object var dateobj = new Date(); // hour from above date object is // being extracted using getUTCHours(). var B = dateobj.getUTCHours(); // Printing current hour according // to universal time. document.write(B); </script> |
输出:
18
支持的浏览器: 支持的浏览器 JavaScript Date getUTCHours()方法 以下列出了:
- 谷歌Chrome 1及以上版本
- 边缘12及以上
- Firefox 1及以上版本
- Internet Explorer 4及以上版本
- 歌剧4及以上
- Safari 1及以上
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END