-5

I have 2 variables in javascript where 2 times has been stored for a reason.

say for ex:

 var currentdate = new Date(); 
    var time1 = "24:00:00"; // everytime this will be 24
    var time2 = var time2 = currentdate.getHours() +":"+ currentdate.getMinutes() ; //  this is the system time

need difference between the 2 times.

If have tried with time1-time2 but it is not working.

I want to make if difference between the 2 times is x, then perform some task. I need the difference thats it.

john z
  • 101
  • 1
  • 1
  • 8
  • You're trying to subtract two string, which will lead to a NaN( Not a Number) – zubair1024 Jul 06 '16 at 16:20
  • I don’t understand what you’re asking. First, complete your code example, don’t cut it off in the middle of a string. Then, specify what you mean by difference. Difference in seconds? As a number? As a string? Difference as a specific time format? What format? Or a `Date` object? A different kind of object? Why did you think `time1 - time2` would work? They’re strings. You can’t just subtract them. If you’re looking for an algorithm to calculate the difference, show your research effort and your own attempts. – Sebastian Simon Jul 06 '16 at 16:20
  • 5
    Possible duplicate of [Check time difference in Javascript](http://stackoverflow.com/questions/1787939/check-time-difference-in-javascript) – Turnip Jul 06 '16 at 16:21
  • it is not duplicate, date is added there, but here only time will be there – john z Jul 06 '16 at 16:25
  • yes zubair, can u please tell me how can i covert that to time... that is how the time is in the requirement... – john z Jul 06 '16 at 16:26
  • updated my question please check it – john z Jul 06 '16 at 17:24
  • why dont you guys read the question properly before giving a statement that it is duplicate question. – john z Jul 06 '16 at 17:31

1 Answers1

0

Something like this should work if your times are in the standard format that counts the date/time as the number of milliseconds from Jan 1, 1970. See here for details.

difference = parseInt(time2) - parseInt(time1)

Lastly, you can set a date to tonight's midnight (the one that has already passed) like this:

var midnight = new Date();
midnight.setHours(0,0,0,0);

Then, use it to subtract your time of interest using the method above where both variables have Date type.

MadPhysicist
  • 5,401
  • 11
  • 42
  • 107
  • As I said, the numbers need to be in the standard form. NaN means "Not a Number." JS does not understand how to treat your entries as numbers. – MadPhysicist Jul 06 '16 at 17:51
  • also, you should be doing `var time2 = new Date();` – MadPhysicist Jul 06 '16 at 17:53
  • but in my case there will no date, only time will be there... is it possible to get difference with only times with out including date? – john z Jul 06 '16 at 17:57
  • Yes, time is also just a number. If you are getting NaN, you need to check your time1 and time2 variables. One of them is not a number. Do a `typeof(time1)` and `typeof(time2)` to your console. – MadPhysicist Jul 06 '16 at 18:03