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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。