下麵是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,並且。
這裏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
相關用法
- JavaScript Date toLocaleTimeString()用法及代碼示例
- JavaScript Date UTC()用法及代碼示例
- JavaScript Date getHours()用法及代碼示例
- Javascript date.getMinutes()用法及代碼示例
- Javascript date.getMonth()用法及代碼示例
- Javascript date.getSeconds()用法及代碼示例
- Javascript date.getDay()用法及代碼示例
- JavaScript Date getMilliseconds()用法及代碼示例
- JavaScript Date getFullYear()用法及代碼示例
- JavaScript Date getDate()用法及代碼示例
- JavaScript Date getUTCDate()用法及代碼示例
- JavaScript Date getUTCDay()用法及代碼示例
- JavaScript Date getUTCFullYear()用法及代碼示例
- JavaScript Date getUTCHours()用法及代碼示例
- JavaScript Date getUTCMilliseconds()用法及代碼示例
- JavaScript Date getUTCSeconds()用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript Date setHours() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。