1、構造函數
Date 對象可以通過構造函數來生成,Date 的構造函數可以放入四種不同的參數
1.1、new Date() ,返回此時的本地日期時間的date對象
let d = new Date(); console.log(d); //輸出 Wed Jul 24 2019 10:26:37 GMT+0800 (中國標准時間)
1.2、new Date(毫秒數) ,返回一個通過毫秒數轉變的date對象
參數里面的毫秒數是整數,表示的是從 '1970/01/01 00:00:00' 到所返回的 date 對象兩者之間相隔的毫秒數。
注意:起點的時分秒還要加上當前所在的時區,如北京時間的時區為東8區,起點時間實際為:'1970/01/01 08:00:00' ,即 Thu Jan 01 1970 08:00:00 GMT+0800 (中國標准時間)
var d1 = new Date(1000 * 60 * 1); // 返回相比標准時間點前進了1分鍾的date對象 console.log(d1); //輸出 Thu Jan 01 1970 08:01:00 GMT+0800 (中國標准時間) 因為是在中國,所以是以東八區作為標准時間點的 d1 = new Date(-1000 * 3600 * 24); // 返回相比標准時間點倒退了1天的date對象 console.log(d1); //輸出 Wed Dec 31 1969 08:00:00 GMT+0800 (中國標准時間)
1.3、new Date(format字符串),返回一個通過字符串轉變的date對象
format 字符串的格式主要有兩種
-
"yyyy/MM/dd HH:mm:ss" (推薦方式):若省略時間,返回的Date對象的時間為 00:00:00。
-
"yyyy-MM-dd HH:mm:ss" :若省略時間,返回的Date對象的時間為 08:00:00(加上本地時區)。若不省略時間,此字符串在IE中會報錯
var dt = new Date('2014/12/25'); console.log(dt); // Thu Dec 25 2014 00:00:00 GMT+0800 (中國標准時間) dt = new Date('2014/12/25 12:00:00'); console.log(dt); // Thu Dec 25 2014 12:00:00 GMT+0800 (中國標准時間) dt = new Date('2014-12-25'); console.log(dt); // Thu Dec 25 2014 08:00:00 GMT+0800 (中國標准時間) dt = new Date('2014-12-25 12:00:00'); // 用這種方式不省略時間時在IE中會報錯! console.log(dt); // Thu Dec 25 2014 12:00:00 GMT+0800 (中國標准時間)
1.4、new Date(year, month, day, hours, minutes, seconds, milliseconds),把年月日、時分秒轉變成date對象
參數:
1)year:整數,4位或者2位數字。如:四位1999表示1999年,兩位97 表示1979年
2)month:整數,2位數字。從0開始計算,0表示1月份、11表示12月份。
3)opt_day:整數,可選,2位數字
4)opt_hours:整數,可選,2位數字,范圍0~23。
5)opt_minutes:整數,可選,2位數字,范圍0~59。
6)opt_seconds:整數,可選,2位數字,范圍0~59。
7)opt_milliseconds:整數,可選,范圍0~999。
var dt = new Date(2014, 11); // 2014年12月(這里輸入的月份數字為11) console.log(dt); // Mon Dec 01 2014 00:00:00 GMT+0800 (中國標准時間) dt = new Date(2014, 11, 25); // 2014年12月25日 console.log(dt); // Thu Dec 25 2014 00:00:00 GMT+0800 (中國標准時間) dt = new Date(2014, 12, 25); // 2014年13月25日(這里輸入的月份數字為12,表示第13個月,跳轉到第二年的1月) console.log(dt); // Sun Jan 25 2015 00:00:00 GMT+0800 (中國標准時間) dt = new Date(2014, 11, 25, 15, 30, 40); // 2014年12月25日 15點30分40秒 console.log(dt); // Thu Dec 25 2014 15:30:40 GMT+0800 (中國標准時間)
2、實例方法
2.1、get 方法
-
getFullYear() :返回Date對象的年份值;4位年份。
-
getMonth() :返回Date對象的月份值。從0開始,所以真實月份=返回值+1 。
-
getDate() :返回Date對象的月份中的日期值;值的范圍1~31 。
-
getHours() :返回Date對象的小時值。
-
getMinutes() :返回Date對象的分鍾值。
-
getSeconds() :返回Date對象的秒數值。
-
getMilliseconds() :返回Date對象的毫秒值。
-
getDay() :返回Date對象的一周中的星期值;0為星期天,1為星期一、2為星期二,依此類推
-
getTime() :返回Date對象與'1970/01/01 00:00:00'之間的毫秒值(北京時間的時區為東8區,起點時間實際為:'1970/01/01 08:00:00') 。
2.2、set 方法
-
setFullYear(year, opt_month, opt_date) :設置Date對象的年份值;4位年份。
-
setMonth(month, opt_date) :設置Date對象的月份值。0表示1月,11表示12月。
-
setDate(date) :設置Date對象的月份中的日期值;值的范圍1~31 。
-
setHours(hour, opt_min, opt_sec, opt_msec) :設置Date對象的小時值。
-
setMinutes(min, opt_sec, opt_msec) :設置Date對象的分鍾值。
-
setSeconds(sec, opt_msec) :設置Date對象的秒數值。
-
setMilliseconds(msec) :設置Date對象的毫秒值。
2.3、其它方法
-
toString() :將Date轉換為一個'年月日 時分秒'字符串
-
toLocaleString() :將Date轉換為一個'年月日 時分秒'的本地格式字符串
-
toDateString() :將Date轉換為一個'年月日'字符串
-
toLocaleDateString() :將Date轉換為一個'年月日'的本地格式字符串
-
toTimeString() :將Date轉換為一個'時分秒'字符串
-
toLocaleTimeString() :將Date轉換為一個'時分秒'的本地格式字符串
-
valueOf() :與getTime()一樣, 返回Date對象與'1970/01/01 00:00:00'之間的毫秒值(北京時間的時區為東8區,起點時間實際為:'1970/01/01 08:00:00')
參考:https://www.cnblogs.com/polk6/p/4156595.html#Menu5-Eg