let now =newDate();alert( now ); // shows current date/time
new Date(milliseconds)
// 0 means 01.01.1970 UTC+0let Jan01_1970 =newDate(0);alert( Jan01_1970 );// now add 24 hours, get 02.01.1970 UTC+0let Jan02_1970 =newDate(24*3600*1000);alert( Jan02_1970 );
new Date(datestring)
// string 用 Date.parse 轉換成時間let date =newDate("2017-01-26");alert(date);
new Date(year, month, date, hours, minutes, seconds, ms)
The year must have 4 digits: 2013 is okay, 98 is not.
The month count starts with 0 (Jan), up to 11 (Dec).
The date parameter is actually the day of month, if absent then 1 is assumed.
If hours/minutes/seconds/ms is absent, they are assumed to be equal 0.
newDate(2011,0,1,0,0,0,0); // // 1 Jan 2011, 00:00:00newDate(2011,0,1); // the same, hours etc are 0 by defaultlet date =newDate(2011,0,1,2,3,4,567);alert( date ); // 1.01.2011, 02:03:04.567
setTime(milliseconds) (sets the whole date by milliseconds since 01.01.1970 UTC)
除了 setTime() 都有 UTC+0 的時間。
let today =newDate();today.setHours(0);alert(today); // still today, but the hour is changed to 0today.setHours(0,0,0,0);alert(today); // still today, now 00:00:00 sharp.
Autocorrection
// date 有自動校對功能let date =newDate(2013,0,32); // 32 Jan 2013 ?!?alert(date); // ...is 1st Feb 2013!// 不用思考是是否為閏月let date =newDate(2016,1,28);date.setDate(date.getDate() +2);alert( date ); // 1 Mar 2016// 取得一段時間後的時間let date =newDate();date.setSeconds(date.getSeconds() +70);alert( date ); // shows the correct date// 可以用零或負數let date =newDate(2016,0,2); // 2 Jan 2016date.setDate(1); // set day 1 of monthalert( date );date.setDate(0); // min day is 1, so the last day of the previous month is assumedalert( date ); // 31 Dec 2015
Date to number, date diff
// 時間轉換成數字,相當於用 date.getTime()let date =newDate();alert(+date); // the number of milliseconds, same as date.getTime()// 日期可以相減,結果為 mslet start =newDate(); // start counting// do the jobfor (let i =0; i <100000; i++) {let doSomething = i * i * i;}let end =newDate(); // donealert( `The loop took ${end - start} ms` );
Date.now()
// 計算時間差,等同 new Date().getTime(),但沒有創造 Date 物件let start =Date.now(); // milliseconds count from 1 Jan 1970// do the jobfor (let i =0; i <100000; i++) {let doSomething = i * i * i;}let end =Date.now(); // donealert( `The loop took ${end - start} ms` ); // subtract numbers, not dates
Benchmarking
// we have date1 and date2, which function faster returns their difference in ms?functiondiffSubtract(date1, date2) {return date2 - date1;}// orfunctiondiffGetTime(date1, date2) {returndate2.getTime() -date1.getTime();}// diffGetTime() 明顯快得多,因為沒有類型轉換,但這並不精確,因為執行 diffSubtract()時,CPU 可能在運行其他東西。functiondiffSubtract(date1, date2) {return date2 - date1;}functiondiffGetTime(date1, date2) {returndate2.getTime() -date1.getTime();}functionbench(f) {let date1 =newDate(0);let date2 =newDate();let start =Date.now();for (let i =0; i <100000; i++) f(date1, date2);returnDate.now() - start;}alert( 'Time of diffSubtract: '+bench(diffSubtract) +'ms' );alert( 'Time of diffGetTime: '+bench(diffGetTime) +'ms' );functiondiffSubtract(date1, date2) {return date2 - date1;}// 多測驗幾次可以增加準確度functiondiffGetTime(date1, date2) {returndate2.getTime() -date1.getTime();}functionbench(f) {let date1 =newDate(0);let date2 =newDate();let start =Date.now();for (let i =0; i <100000; i++) f(date1, date2);returnDate.now() - start;}let time1 =0;let time2 =0;// run bench(upperSlice) and bench(upperLoop) each 10 times alternatingfor (let i =0; i <10; i++) { time1 +=bench(diffSubtract); time2 +=bench(diffGetTime);}alert( 'Total time for diffSubtract: '+ time1 );alert( 'Total time for diffGetTime: '+ time2 );// JavaScript 對執行很多次的程式碼有優化的效果,因此要將上一個加溫效果// added for "heating up" prior to the main loopbench(diffSubtract);bench(diffGetTime);// now benchmarkfor (let i =0; i <10; i++) { time1 +=bench(diffSubtract); time2 +=bench(diffGetTime);}
Date.parse from a string
YYYY-MM-DD – is the date: year-month-day.
The character "T" is used as the delimiter.
HH:mm:ss.sss – is the time: hours, minutes, seconds and milliseconds.
The optional 'Z' part denotes the time zone in the format +-hh:mm. A single letter Z that would mean UTC+0.
let ms =Date.parse('2012-01-26T13:51:50.417-07:00');alert(ms); // 1327611110417 (timestamp)let date =newDate( Date.parse('2012-01-26T13:51:50.417-07:00') );alert(date);