Javascript date
現在毫秒數(把時分秒算進去)
new Date().getTime()
Step1: new Date() => Tue Feb 11 2020 14:29:05
今天毫秒數 1581391231183
new Date(new Date().toISOString().slice(0,10).replace(/-/g,"/")).getTime()
step1 : new Date().toISOString().slice(0,10).replace(/-/g,"/") => 2020/02/11[今日]
step2 : new Date('2020/02/11').getTime()
step3 : 1581350400000
1天86400000 毫秒數
(1000 * 60 * 60 * 24)
兩天毫秒數 172800000
(1000 * 60 * 60 * 24) *2
兩天前毫秒數:1581218431183
1581391231183 - 172800000 = 1581218431183
兩天前日期:1581218431183
new Date(1581218431183)
new Date(new Date(1581391231183 - 172800000))
=> Sun Feb 09 2020 11:20:31 GMT+0800 (台北標準時間)
格式化
new Date(new Date(1581391231183 - 172800000)).toISOString().slice(0, 10).replace(/-/g, "/")
replace(/-/g, "/") => https://larry850806.github.io/2016/06/23/regex/
JS今天日期
new Date()
=> Tue Feb 11 2020 11:31:21 GMT+0800 (台北標準時間)
顯示前N天的日期
//N=1
var day = 1;
var Date = new Date(new Date().getTime() - (86400000 * day)).toISOString().slice(0, 10).replace(/-/g, "/");
其他:
new Date('2020/02/15 09:00').getTime() =>1581728400000
new Date(1581728400000) => Sat Feb 15 2020 09:00:00 GMT+0800 (台北標準時間)
參考連結:
JavaScript 毫秒轉換成日期:Milliseconds to Date in JavaScript
http://www.eion.com.tw/Blogger/?Pid=1148
JavaScript 許多靈異事件
https://blog.miniasp.com/post/2016/09/25/JavaScript-Date-usage-in-details