New Date()與setDate()參數
相信網上已經有很多關於日期的文章了,這里只是我自己再工作中遇到的問題然后加以總結;
new Date()
new Date() 一共有六種形式,五種帶參數的一種不帶參數的;
- new Date();自然不用多說,默認獲取的是當前日期。
- new Date("month1 dd,yyyy hh:mm:ss"); 注意:參數是字符形式
- new Date("month1 dd,yyyy"); 注意:參數是字符形式
- new Date(yyyy,month2,dd,hh,mm,ss); 注意:參數不是字符
- new Date(yyyy,month2,dd); 注意:參數不是字符
- new Date(ms);
參數說明:
month1:用英文,表示月份名稱;從January到December ;
dd:表示日期,1-31
yyyy:表示四位表示的年份
hh:mm:ss:表示時間,時(0-23)-分(0-59)-秒(0-59)
month2:是Number型的月份;從0-11;即1月到12月
ms:從1970年1月1日之間相差的毫秒數
特別提醒:有些是字符形式有些不是
setDate()
我們在工作中經常用setDate()實現日期的相加減;setDate()接收一個整數,如果這個整數大於當前時間對象中月份的日期上線,則自動往前進位,余數為下個月的日期
var d=new Date(2019,1,21);
d.setDate(d.getDate()+15);
特別提醒:setDate()接收的是一個整數
分類:
JS