0

i'm trying to calculate the hours difference between to times using javascript. But i keep get the results NaN in the console. I get the current time using javascript and the late time from the 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))

the time from the localstorage reads 15/7/2017 9:30:46

user6579134
  • 749
  • 3
  • 10
  • 35

3 Answers3

0

localStorage will store stringified version of any object, you need to parse it. If you converted it to milliseconds then also you need to parse it to number, it can save only string

var earlierDate = new Date( localStorage.getItem('login_time'))
// or var earlierDate = parseInt(localStorage.getItem('login_time'))
var currentDate = new Date()
var diff = currentDate - earlierDate;

Then convert diff to hour/minutes/seconds with your logic

Koushik Chatterjee
  • 4,106
  • 3
  • 18
  • 32
0

Im not shure what youre trying to do here:

date2 - date1

These are booth strings, you cannot substract them. However you might convert them to milliseconds since 1970 which you could then do Math on:

var log_time = localStorage.getItem('login_time').split(" ");

log_time[0]=log_time[0].split("/").reverse().map((el,i)=>i?("0"+el).slice(-2):el).join("-");//5/4/2017 => 2017-04-05
log_time[1]=("0"+log_time[1]).slice(-8);// 9:30:10 => 09:30:10

var difference= new Date() - new Date(log_time.join("T"));
var hours=Math.floor(difference/(1000*60*60 /* one hour in ms */));

You may overthink the stored format. Its quite complicated to parse it properly.

http://jsbin.com/fofowayata/edit?console

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

You need to change your date format little bit This may Help you and also parse those dates because those are stirng formate.

Working Fiddle

var log_time1 = '2017-07-15 09:30:46';//Examples of ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS.

var log_time = new Date(log_time1)//string parsing date

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


var today = year + "-" + month + "-" + day
var time =  hour + ":" + minute + ":" + second
var date1 = (log_time);
var test_date2 = (today+' '+time);

var date2= new Date(test_date2);//string parsing date

var hours = Math.abs(date2 - date1) / 36e5;
alert(hours.toFixed(2)) 
Minar Mnr
  • 1,376
  • 1
  • 16
  • 23