当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JavaScript Date setHours()用法及代码示例


下面是Date setHours()方法的示例。

  • 例:
    <script> 
       // Here a date has been assigned 
       // while creating Date object 
       var dateobj =  
       new Date('October 13, 1996 05:35:32'); 
      
       // New hour 11 is being set in above Date 
       // Object with the help of setHours() method 
       dateobj.setHours(11); 
      
       // New hour from above Date Object is 
       // being extracted using getHours() 
       var B = dateobj.getHours(); 
      
       // Printing new hour 
       document.write(B); 
    </script>
  • 输出:
    11

date.setHours()方法用于将小时设置为使用Date()构造函数创建的日期对象。

用法:

DateObj.setHours(hours_Value)

参数:该方法接受上述和以下描述的单个参数:

  • hours_Value:此参数保存用于在Date()构造函数中设置的小时值。

返回值:它返回由setHours()方法设置的具有更新小时数的新日期。



注意:DateObj是使用Date()构造函数创建的有效Date对象,我们要在其中构造小时数。

上述方法的更多代码如下:

程序1:如果在Date()构造函数中我们在创建Date对象时没有给出小时,则setHours()方法仍将设置作为其参数给出的新小时。

<script> 
   // Here hour has not been assigned 
   // while creating Date object 
   var dateobj = new Date('October 13, 1996'); 
  
   // New hour 11 is being set in above Date 
   // Object with the help of setHours() method 
   dateobj.setHours(11); 
  
   // New hour from above Date Object is 
   // being extracted using getHours() 
   var B = dateobj.getHours(); 
  
   // Printing new hour 
   document.write(B); 
</script>

输出:

11

范例2:如果Date()构造函数中未提供任何参数,则setHours()方法仍设置小时,但月份,年份和日期将是当前月份,年份和日期。这里11是新小时,2是当前月,即3月,30是当前日期,2018是当前年。

<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 setHours() method 
   dateobj.setHours(11); 
  
   // Hour from above Date Object is 
   // being extracted using getHours() 
   var B = dateobj.getHours(); 
  
   // Month from above Date Object is 
   // being extracted using getMonth() 
   var C = dateobj.getMonth(); 
  
   // Date from above Date Object is 
   // being extracted using getDate() 
   var D = dateobj.getDate(); 
  
   // Year from above Date Object is 
   // being extracted using getFullYear() 
   var E = dateobj.getFullYear(); 
  
   // 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

范例3:如果在setHours()方法的参数中将小时值设置为26,则将小时设置为2,因为小时范围是从0到23,并且26%24=2

这里2是新小时,9是月,即10月,14是日期,年份是1996。在这里,我们看到13是原始日期,但输出变为14,因为给定方法的参数将26小时转换为2第二天的小时数,这就是为什么日期增加1,即从13到14。

<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 setHours() method 
   dateobj.setHours(26); 
  
   // Hour from above Date Object is 
   // being extracted using getHours() 
   var B = dateobj.getHours(); 
  
   // Month from above Date Object is 
   // being extracted using getMonth() 
   var C = dateobj.getMonth(); 
  
   // Date from above Date Object is 
   // being extracted using getDate() 
   var D = dateobj.getDate(); 
  
   // Year from above Date Object is 
   // being extracted using getFullYear() 
   var E = dateobj.getFullYear(); 
  
   // 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 setHours()方法支持的浏览器:

  • 谷歌浏览器
  • IE浏览器
  • 火狐浏览器
  • Opera
  • Safari




相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 JavaScript Date setHours() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。