我試圖計算使用JavaScript時間之間的差異。但我一直在控制檯中得到NaN的結果。我使用JavaScript和後期時間從localStorage的小時的時差與小數點後兩位小數點差
var log_time = localStorage.getItem('login_time')
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
var hour = currentDate.getHours(); // => 9
var minute= currentDate.getMinutes(); // => 30
var second= currentDate.getSeconds(); // => 51
console.log(log_time);
var today = day + "/" + month + "/" + year
var time = hour + ":" + minute + ":" + second
console.log(today+' '+time);
var date1 = (log_time);
var date2 = (today+' '+time);
var hours = Math.abs(date2 - date1)/36e5;
console.log(hours.toFixed(2))
時從localstorage
讀取15/7/2017 9:30:46
什麼是你想要的輸出? –
'date2'是一個字符串,'date1'是一個字符串,當你減去兩個字符串時,你會發生什麼? –