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


Javascript date.setUTCDate()用法及代碼示例

date.setUTCDate()是JavaScript中的內置函數,用於根據通用時間將一個月的日期設置為使用Date()構造函數創建的日期對象。

用法:

DateObj.setUTCDate(date_Value);

DateObj是使用Date()構造函數創建的有效Date對象,在該構造函數中,我們要根據通用時間設置一個月的日期。


參數:這裏的參數date_Value是要在使用Date()構造函數創建的日期對象中設置的日期值。

返回值:它返回由setUTCDate()函數設置的新的月更新日期,該月的日期是1到31之間的整數值。

以下示例程序旨在說明setITCDate()函數:

<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 date 15 of the month is being set in above Date 
// Object with the help of setDate() function 
dateobj.setUTCDate(15); 
  
// new date of the month according to universal time from 
// above Date Object is being extracted using getDate() 
var B = dateobj.getUTCDate(); 
  
// Printing new date of the month 
document.write(B); 
</script>

輸出:

15

錯誤和異常

  • 示例1:在這裏,我們知道月份的日期在1到31之間,但是如果我們嘗試將日期設置為33,則它將下個月的日期設置為2,因為33-31 = 2,並且此日期2成為下一個日期上個月的。
    <script> 
    // Here a date according to universal time has 
    // been assigned while creating Date object 
    var dateobj = new Date('October 13, 1996 05:35:32 GMT-3:00'); 
      
    // new date 33 of the month is being set in above Date 
    // Object with the help of setUTCDate() function 
    dateobj.setUTCDate(33); 
      
    // new date of the month from above Date Object is 
    // being extracted using getUTCDate() 
    var B = dateobj.getUTCDate(); 
      
    // new month from above Date Object is 
    // being extracted using getUTCMonth() 
    var C = dateobj.getUTCMonth(); 
      
    // Printing new date of the month 
    document.write(B + "<br>"); 
      
    // Printing new month 
    document.write(C); 
    </script>

    輸出:

    2
    10

    在上麵的輸出中,2是十一月的日期,而10是十一月的月份,因為月份名稱從0到11開始,即,一月為0,十二月為11。

  • 示例2:如果在Date()構造函數中我們在創建日期對象時未提供月份的任何日期,則setUTCDate()函數仍將能夠設置新日期,該日期作為其在創建的日期對象中的參數給出。
    <script> 
    // Here date according to universal time has 
    // not been assigned while creating Date object 
    var dateobj = new Date('October, 1996 05:35:32 GMT-3:00'); 
      
    // new date 12 of the month is being set in above Date 
    // Object with the help of setUTCDate() function 
    dateobj.setUTCDate(12); 
      
    // new date of the month from above Date Object is 
    // being extracted using getUTCDate() 
    var B = dateobj.getUTCDate(); 
      
    // Printing new date of the month 
    document.write(B); 
    </script>

    輸出:

    12
  • 示例3:如果Date()構造函數中未提供任何參數,則setDate()函數仍然可以設置日期,而月份仍為當前日期。
    <script> 
    // Here nothing has been assigned 
    // while creating Date object 
    var dateobj = new Date(); 
      
    // new date 13 of the month is being set in above Date 
    // Object with the help of setUTCDate() function 
    dateobj.setUTCDate(13); 
      
    // date of the month from above Date Object is 
    // being extracted using getUTCDate() 
    var B = dateobj.getUTCDate(); 
      
    // month from above Date Object is 
    // being extracted using getUTCMonth() 
    var C = dateobj.getUTCMonth(); 
      
    // Printing new date of the month 
    document.write(B + "<br>"); 
      
    // Printing new month 
    document.write(C); 
    </script>

    輸出:

    13
    3
  • 示例4:如果將月份的新日期設置為零(0),則新日期將設置為上個月的最後一天。
    <script> 
    // Here a date according to universal time 
    // has been assigned while creating Date object 
    var dateobj = new Date('October 13, 1996 05:35:32 GMT-3:00'); 
      
    // new date 0 of the month is being set in above Date 
    // Object with the help of setUTCDate() function 
    dateobj.setUTCDate(0); 
      
    // date of the month from above Date Object is 
    // being extracted using getUTCDate() 
    var B = dateobj.getUTCDate(); 
      
    // month from above Date Object is 
    // being extracted using getUTCMonth() 
    var C = dateobj.getUTCMonth(); 
      
    // Printing new date of the month 
    document.write(B + "<br>"); 
      
    // Printing new month 
    document.write(C); 
    </script>

    輸出:

    30
    8

支持的瀏覽器:JavaScript date.setUTCDate()函數支持的瀏覽器如下:

  • 穀歌瀏覽器
  • IE瀏覽器
  • 火狐瀏覽器
  • Opera
  • 蘋果瀏覽器


相關用法


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