JavaScript Date setUTCHours()方法

下面是一个例子 日期设置() 方法

null
  • 例子:

    <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() method
    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

这个 日期”() 方法用于根据使用date()构造函数创建的通用时间将小时设置为日期对象。

语法:

DateObj.setUTCHours(hours_Value);

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

  • 小时价值: 此参数保存小时值,该值用于在使用date()构造函数创建的date对象中设置。

    返回值: 它返回由setUTCHours()方法设置的新的即更新的小时数。

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

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

    项目1: 如果在Date()构造函数中,我们不提供 小时 在创建Date对象时,still 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() method
    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()构造函数中没有给出任何参数,still setUTCHours()方法将能够设置小时,但根据世界时间,月份、年份和日期仍然是当前的。 这里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 setUTCHours() method
    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

    方案3: 如果将hour的值设为26作为setHours()方法的参数,它会将2设为小时,因为小时范围为0到23,因此 26-24=2 ,这里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() method
    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()方法 以下列出了:

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