js/ts 計算兩段時間相差多少天

 

let n_date = new Date();
let l_str: string[] = '2020/1/1'.split("/");           //過去的時間
let n_str: string[] = n_date.toLocaleDateString().split("/"); //當前的時間-- 格式:2020/12/31  
let oDate1 = new Date(((l_str[1] + '-' + l_str[2] + '-' + l_str[0]).replace(/-/g, "/"))); 
let oDate2 = new Date(((n_str[1] + '-' + n_str[2] + '-' + n_str[0]).replace(/-/g, "/")));
let iDays = Math.floor(Math.abs(oDate1.getTime() - oDate2.getTime()) / 1000 / 60 / 60 / 24);

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章