new Date(year, month, day, hours, minutes, seconds, milliseconds)習慣寫 C# 的我,很直覺地認為建構式的參數和 C# 一樣,but 人生最厲害的就是這個 but,就因為這個建構式,花了我好幾個小時的時間才找到錯誤,看來偵錯的能力還有待加強。
日期函數中的月份是從 0 到 11,就因為沒有注意到這一點,所以當我把他像 C# 一樣使用的時候,我拿到物件的日期總是多加了一個月,但是我又把此物件轉成 Timesamp (Unix epoch time),所以就一直沒有注意到產生的時間錯了,下次看 W3C School 的時候,要特別注意下方黃色的說明。
日期類別注意事項:
- JavaScript dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC).
- JavaScript counts months from 0 to 11. January is 0. December is 11.
- When setting a date, without specifying the time zone, JavaScript will use the browser's time zone.
- When getting a date, without specifying the time zone, the result is converted to the browser's time zone.
另外,常用到的建構式 new Date(dateString) 其 dateString 格式為: "2015-03-25T12:00:00",字串中 T 代表的是 UTC 時間;然而此時間透過 toString() 方法得到的文字會依照瀏覽器的時區來表示時間,例如: new Date("2015-03-25T12:00:00").toString() 在我的瀏覽器 (GMT+8) 會得到以下字串:
Wed Mar 25 2015 20:00:00 GMT+0800 (台北標準時間)
沒有留言:
張貼留言