Date對象用於處理日期和時間segmentfault
myDate獲得一個時間對象Object,會自動把本地當前日期和時間保存爲其初始值code
var myDate = new Date(); // myDate: Mon Mar 06 2017 09:41:37 GMT+0800 (中國標準時間)
(1). 數字的形式:new Date(年,月,日,時,分,秒)對象
1. var myDate = new Date(2017,3,6,10,0,0); //myDate: Thu Apr 06 2017 10:00:00 GMT+0800 (中國標準時間) 2. var myDate = new Date(17,3,6,10,0,0); //myDate: Fri Apr 06 1917 10:00:00 GMT+0800 (中國標準時間) 3. var myDate = new Date(2017,3,6,25,0,0); //myDate: Fri Apr 07 2017 01:00:00 GMT+0800 (中國標準時間)
注意:ip
月份接收的參數是0~11(1月~12月),因此傳入3,獲得4月份字符串
年份應該傳入4位數,不然會自動在參數的基礎上加入1990來表示最終的年份get
傳入的月數超過天然月數,會自動向年份進位,天數超過天然天數會向月份進位,以此類推基礎
(2). 字符串的形式:new Date('月,日,年 時:分:秒')方法
1. var myDate = new Date('March,7,2017 1:20:00'); //myDate: Tue Mar 07 2017 01:20:00 GMT+0800 (中國標準時間) 2. var myDate = new Date('March,2017,7 1:20:00'); //myDate: Tue Mar 07 2017 01:20:00 GMT+0800 (中國標準時間)
注意:im
月份需傳入英文單詞 (January、February、March、April、May、June、July、August、September、October、November、December)時間戳
年和日能夠互換位置,時分秒固定用:分割
(3). 時間戳的形式: new Date(1488766860327)
var myDate = new Date(1488766860327); //myDate: Mon Mar 06 2017 10:21:00 GMT+0800 (中國標準時間)
(1)getFullYear(): 返回四位數字的年份(Number類型)
new Date().getFullYear(); //2017 Number類型
(2)getMonth(): 返回月份 (Number類型 0~11)
new Date('March,2017,7 1:20:00').getMonth(); //2 Number類型
(3)getDate(): 返回月份中的某一天 (Number類型的整數)
new Date().getDate() //6 Number類型
(4)getHours(): 返回時間的小時 (Number類型)
new Date('March,2017,7 1:20:00').getHours() //1 Number類型
(5)getMinutes(): 返回時間的分 (Number類型)
new Date('March,2017,7 1:20:00').getMinutes() //20 Number類型
(6)getSeconds(): 返回時間的秒 (Number類型)
new Date('March,2017,7 1:20:00').getSeconds() //0 Number類型
(7)getTime(): 返回距 1970 年 1 月 1 日之間的毫秒數 (Number類型)
new Date().getTime() //1488768275894 Number類型
(8)getDay(): 返回星期的某一天的數字 (0-6)
new Date().getDay() //1~6表明週一到週六 0表明周天
1.比較時間的大小
2.將時間戳轉化成指定格式的日期
3.求時間差(天、時、分、秒)
示例詳見系列文章:Javascript Date經常使用示例