I need to compare two different datetime strings (formed: YYYY-MM-DD'T'HH:mm).
Here are the datetime strings:
var a = ("2017-05-02T10:45");
var b = ("2017-05-02T12:15");
I've sliced the dates out of them so I only need the time (formed: HH:mm).
var now = a.slice(11, 16);
var then = b.slice(11, 16);
// now = 10:45
// then = 12:15
Is there any way I could get the difference between these two times?
Result should look like this:
1 hour 30 minutes
Also if the dates are different is there any easy solution to get the date difference too?
To compare two time strings:Get the hour, minute and seconds value from each string. Use the values to create a Date object. Compare the output from calling the getTime() method on the Date objects.
In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the Date into a numeric value by using the getTime() function. By converting the given dates into numeric values we can directly compare them.
To calculate the number of hours between two dates we can simply subtract the two values and multiply by 24.
Use javascript Date:
var a = ("2017-05-02T10:45");
var b = ("2017-05-02T12:15");
var milliseconds = ((new Date(a)) - (new Date(b)));
var minutes = milliseconds / (60000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With