date.setMinutes()是JavaScript中的內置函數,用於將分鍾設置為使用Date()構造函數創建的Date對象。
用法:
DateObj.setMinutes(Minutes_Value)
DateObj是使用Date()構造函數創建的有效Date對象,我們要在其中設置分鍾。分鍾的值是從0到59。
參數:這裏的參數minutes_Value是分鍾的值,用於在Date()構造函數中設置。
返回值:它返回由setMinutes()函數設置的具有更新分鍾數的新日期。
下麵的程序演示了setMinutes()函數:
<script>
// Here a date has been assigned
// while creating Date object
var dateobj = new Date('October 13, 1996 05:35:32');
// new minute of 52 is being set in above Date
// Object with the help of setMinutes() function
dateobj.setMinutes(52);
// new minute from above Date Object is
// being extracted using getMinutes()
var B = dateobj.getMinutes();
// Printing new minute
document.write(B);
</script>
輸出:
52
錯誤和異常
- 示例1:如果在Date()構造函數中我們在創建Date對象時沒有給出任何分鍾,則setMinutes()函數仍然設置新分鍾作為其參數。
<script> // Here minute has not been assigned // while creating Date object var dateobj = new Date('October 13, 1996'); // new minute of 51 is being set in above Date // Object with the help of setMinutes() function dateobj.setMinutes(51); // new minute from above Date Object is // being extracted using getMinutes() var B = dateobj.getMinutes(); // Printing new minute document.write(B); </script>
輸出:
51
- 示例2:如果在Date()構造函數中未提供任何參數作為參數,則仍將setMinutes()函數設置為分鍾,但月份,年份,日期等變為當前值。
<script> // Here nothing has been assigned // while creating Date object var dateobj = new Date(); // new minute of 42 is being set in above Date // Object with the help of setMinutes() function dateobj.setMinutes(42); // Minutes from above Date Object is // being extracted using getMinutes() var B = dateobj.getMinutes(); // 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 minutes 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>
輸出:
42 3 1 2018
這裏42是新分鍾,3是當前月,即4月,1是當前日期,2018是當前年。
- 示例3:如果按照setMinutes()函數的參數給定的分鍾值是66,則由於分鍾範圍是0到59和(66%60 = 6),所以它將6設置為分鍾。
<script> // Here date has been assigned // while creating Date object var dateobj = new Date('October 13, 1996 05:35:32'); // new minute of 66 is being set in above Date // Object with the help of setMinutes() function dateobj.setMinutes(66); // minute from above Date Object is // being extracted using getMinutes() var B = dateobj.getMinutes(); // Hour from above Date Object is // being extracted using getHours() var C = dateobj.getHours(); // Printing new minute document.write(B + "<br>"); // Printing hour document.write(C); </script>
輸出:
6 6
在這裏6是新的分鍾,而小時從05變為06,因為分鍾範圍是從0到59,即總計60,我們將新分鍾設置為66,這使小時從05增加1到06,分鍾變成6。
支持的瀏覽器:JavaScript date.setMinutes()函數支持的瀏覽器如下:
- 穀歌瀏覽器
- IE瀏覽器
- 火狐瀏覽器
- Opera
- 蘋果瀏覽器
相關用法
- Javascript Math.pow( )用法及代碼示例
- Javascript Array some()用法及代碼示例
- Javascript Number()用法及代碼示例
- Javascript Symbol.for()用法及代碼示例
- Javascript toExponential()用法及代碼示例
- Javascript toString()用法及代碼示例
- Javascript Math.abs( )用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript | date.setMinutes() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。