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


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


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

  • 例:
    <script> 
       // Here a date has been assigned 
       // while creating Date object 
       var dateobj =  
       new Date('October 13, 1996 05:35:32'); 
      
       // new date 15 of the month is being set 
       // in above Date Object with the help of  
       // setDate() method 
       dateobj.setDate(15); 
      
       // new date of the month from above Date 
       // Object is being extracted using getDate() 
       var B = dateobj.getDate(); 
      
       // Printing new date of the month 
       document.write(B); 
    </script>
  • 輸出:
    15

date.setDate()方法用於將一個月的日期設置為使用date()構造函數創建的日期對象。句法:

dateObj.setDate(date_Value);

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

  • date_Value:它返回由setDate()方法設置的新的月更新日期。月份的日期是一個整數值,範圍是1到31。

返回值:此方法返回日期對象與1970年1月1日午夜之間的毫秒數

注意:DateObj是使用Date()構造函數創建的有效Date對象,我們要在其中設置一個月的日期。



上述方法的更多代碼如下:
程序1:在這裏,我們知道該月的日期在1到31之間,但是如果我們假設將日期設置為33,它將為下個月設置日期2,因為33-31=2並且此日期2成為上個月下一個日期。在下麵的輸出中,2是11月的日期,10是11月,因為月份名稱從0到11開始,即1月為0,12月為11。

<script> 
   // Here a date has been assigned 
   // while creating Date object 
   var dateobj =  
   new Date('October 13, 1996 05:35:32'); 
  
   // New date 33 of the month is being set  
   // in above Date Object with the help of  
   // setDate() method 
   dateobj.setDate(33); 
  
   // new date of the month from above Date 
   // Object is being extracted using getDate() 
   var B = dateobj.getDate(); 
  
   // new month from above Date Object is 
   // being extracted using getMonth() 
   var C = dateobj.getMonth(); 
  
   // Printing new date of the month 
   document.write(B); 
   document.write("<br />"); 
   // Printing new month 
   document.write(C); 
</script>

輸出:

2
10

範例2:如果在Date()構造函數中我們沒有給出月份的任何日期,則setDate()方法仍然設置新日期作為其參數。

<script> 
   // Here date has not been assigned 
   // while creating Date object 
   var dateobj =  
   new Date('October, 1996 05:35:32'); 
  
   // New date 12 of the month is being set  
   // in above Date Object with the help of  
   // setDate() method 
   dateobj.setDate(12); 
  
   // new date of the month from above Date  
   // Object is being extracted using getDate() 
   var B = dateobj.getDate(); 
  
   // Printing new date of the month 
   document.write(B); 
</script>

輸出:

12

範例3:如果在Date()構造函數中未提供任何參數作為參數,則setDate()方法設置日期仍為,但月份變為當前月份。

<script> 
   // Here a date 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  
   // setDate() method 
   dateobj.setDate(13); 
  
   // Date of the month from above Date Object 
   // is being extracted using getDate() 
   var B = dateobj.getDate(); 
  
   // Month from above Date Object is 
   // being extracted using getMonth() 
   var C = dateobj.getMonth(); 
  
   // Printing new date of the month 
   document.write(B + "<be>"); 
   document.write("<br />"); 
   // Printing new month 
   document.write(C);                     
</script>

輸出:

13
2

範例4:如果該月的新日期設置為零(0),則新日期將設置為上個月的最後一天。

<script> 
   // Here a date has been assigned 
   // while creating Date object 
   var dateobj =  
   new Date('October 13, 1996 05:35:32'); 
  
   // new date 0 of the month is being set  
   // in above Date Object with the help of  
   // setDate() method 
   dateobj.setDate(0); 
  
   // Date of the month from above Date Object  
   // is being extracted using getDate() 
   var B = dateobj.getDate(); 
  
   // Month from above Date Object is 
   // being extracted using getMonth() 
   var C = dateobj.getMonth(); 
  
   // Printing new date of the month 
   document.write(B + "<be>"); 
   document.write("<br />"); 
   // Printing new month 
   document.write(C); 
</script>

輸出:

30
8

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

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




相關用法


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