當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


JavaScript Date setUTCMinutes()用法及代碼示例


下麵是Date setUTCMinutes()方法的示例。

  • 例:
    <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 minute of 52 is being set in above Date 
       // Object with the help of setUTCMinutes() method 
       dateobj.setUTCMinutes(52); 
      
       // New minute from above Date Object is 
       // being extracted using getUTCMinutes() 
       var B = dateobj.getUTCMinutes(); 
      
       // Printing new minute 
       document.write(B); 
    </script>
  • 輸出:
    52

date.setUTCMinutes()方法用於根據通用時間將分鍾設置為使用Date()構造函數創建的日期對象。

用法:

DateObj.setUTCMinutes(Minutes_Value);

參數:該方法接受上述和以下描述的單個參數:

  • minutes_Value:此參數保存我們要在使用Date()構造函數創建的日期中設置的分鍾值。

返回值:它返回由setUTCMinutes()方法設置的新的即更新的分鍾。



注意:DateObj是使用Date()構造函數創建的有效Date對象,我們要在其中設置分鍾。分鍾的值是從0到59。

上述方法的更多代碼如下:
範例1:如果在Date()構造函數中創建日期對象時沒有給出任何分鍾,則setUTCMinutes()方法仍然可以根據創建的Date對象中的通用時間設置新分鍾。

<script> 
   // Here minute has not been assigned according to 
   // universal time while creating Date object 
   var dateobj = new Date('October 13, 1996 GMT-3:00'); 
  
   // New minute of 51 is being set in above Date 
   // Object with the help of setUTCMinutes() method 
   dateobj.setUTCMinutes(51); 
  
   // New minute from above Date Object is 
   // being extracted using getUTCMinutes() 
   var B = dateobj.getUTCMinutes(); 
  
   // Printing new minute 
   document.write(B); 
</script>

輸出:

51

範例2:如果在Date()構造函數中未提供任何參數作為參數,則仍然可以設置setUTCMinutes()方法為分鍾,但是月份,年份,日期等仍為當前值。
這裏42是新分鍾,3是當前月,即4月,1是當前日期,2018是當前年。

<script> 
   // Here nothing has been assigned according to 
   // universal time while creating Date object 
   var dateobj = new Date(); 
  
   // New minute of 42 is being set in above Date 
   // Object with the help of setUTCMinutes() method 
   dateobj.setUTCMinutes(42); 
  
   // Minutes from above Date Object is 
   // being extracted using getUTCMinutes() 
   var B = dateobj.getUTCMinutes(); 
  
   // 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 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

範例3:如果將分鍾值設為66作為setUTCMinutes()方法的參數,則它將6設置為分鍾,因為分鍾範圍是從0到59,因此66-60=6,這裏減去60,因為0到59是60。

<script> 
   // Here 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 minute of 66 is being set in above Date 
   // Object with the help of setUTCMinutes() method 
   dateobj.setUTCMinutes(66); 
  
   // Minute from above Date Object is 
   // being extracted using getUTCMinutes() 
   var B = dateobj.getUTCMinutes(); 
  
   // Hour from above Date Object is 
   // being extracted using getUTCHours() 
   var C = dateobj.getUTCHours(); 
  
   // Printing new minute 
   document.write(B + "<br />"); 
  
   // Printing hour 
   document.write(C); 
</script>

輸出:

6
6

支持的瀏覽器:下麵列出了JavaScript Date setUTCMinutes()方法支持的瀏覽器:

  • 穀歌瀏覽器
  • IE瀏覽器
  • 火狐瀏覽器
  • Opera
  • Safari




相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript Date setUTCMinutes() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。