Date and time
內建的物件 Date,可以創造、改變、計算時間。
Creation
new Date()
new Date(milliseconds)
new Date(datestring)
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 with0
(Jan), up to11
(Dec).The
date
parameter is actually the day of month, if absent then1
is assumed.If
hours/minutes/seconds/ms
is absent, they are assumed to be equal0
.
Access date components
getFullYear() 取得年份
getMonth() 取得月份 0 - 11
getDate() 取得日期 1 - 31
getHours(), getMinutes(), getSeconds(), getMilliseconds() 取得對應的時間
getDay() 取得星期 0 (sunday) - 6 (saturday)
以上方法都是返回當地時間, getUTCFullYear(), getUTCMonth(), getUTCDay() 得到 UTC+0 時間
getTime() 從 1970-1-1 00:00:00 UTC+0 開始的 milliseconds
getTimezoneOffset() 返回跟 UTC+0 的時間差(分鐘)
setFullYear(year [, month, date])
setMonth(month [, date])
setDate(date)
setHours(hour [, min, sec, ms])
setMinutes(min [, sec, ms])
setSeconds(sec [, ms])
setMilliseconds(ms)
setTime(milliseconds) (sets the whole date by milliseconds since 01.01.1970 UTC)
除了 setTime() 都有 UTC+0 的時間。
Autocorrection
Date to number, date diff
Date.now()
Benchmarking
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 letterZ
that would mean UTC+0.
Last updated
Was this helpful?