date.setHours()是JavaScript中的內置函數,用於將小時設置為使用Date()構造函數創建的日期對象。
用法:
DateObj.setHours(hours_Value)
DateObj是使用Date()構造函數創建的有效Date對象,我們要在其中設置小時數。
參數:這裏的參數hours_Value是小時值,用於在Date()構造函數中進行設置。
返回值:它返回由setHours()函數設置的具有更新小時數的新日期。
下麵的程序演示了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() function
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
錯誤和異常
- 示例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() function 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()函數仍設置小時,但月,年和日期將為當前月,年和日期。
<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() function 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 + "<br>"); </script>
輸出:
11 2 30 2018
這裏11是新小時,2是當前月,即3月,30是當前日期,2018是當前年。
- 示例3:如果在setHours()函數的參數中將小時值設置為26,則將小時設置為2,因為小時範圍是從0到23的形式,並且。
<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() function 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
這裏2是新小時,9是月份,即10月,14是日期,年份是1996.在這裏,我們看到13是原始日期,但輸出變為14,因為給定26小時作為函數的參數轉換為2第二天的小時數,這就是為什麽日期增加1,即從13到14。
支持的瀏覽器:JavaScript date.setHours()函數支持的瀏覽器如下:
- 穀歌瀏覽器
- IE瀏覽器
- 火狐瀏覽器
- Opera
- 蘋果瀏覽器
相關用法
- Javascript Math.pow( )用法及代碼示例
- Javascript Array some()用法及代碼示例
- Javascript Number()用法及代碼示例
- Javascript Symbol.for()用法及代碼示例
- Javascript toExponential()用法及代碼示例
- Javascript toString()用法及代碼示例
- Javascript Math.abs( )用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript | date.setHours() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。