JavaScript Date setHours()方法

下面是一个例子 日期设定时间() 方法

null
  • 例子:

    <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()构造函数创建的日期对象中设置小时数。

语法:

DateObj.setHours(hours_Value)

参数: 此方法接受一个参数,如上所述,如下所述:

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

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

注: 这个 DateObj 是使用Date()构造函数创建的有效日期对象,我们希望在其中设置小时数。

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

项目1: 如果在Date()构造函数中,我们在创建Date对象时没有给出hour,那么still setHours()方法将设置作为其参数给出的新hour。

<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()方法set hour但月份、年份和日期将是当前的月份、年份和日期。这里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()方法的参数中给出了hour为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()方法 以下列出了:

  • 谷歌Chrome 1及以上版本
  • 边缘12及以上
  • Firefox 1及以上版本
  • Internet Explorer 3及以上版本
  • 歌剧3及以上
  • Safari 1及以上
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享