Jump to content

Subtracting


jimfog

Recommended Posts

If i wanted to subtract one time from the other in javascript what approach i must take? Suppose for example that i want to find the time difference between 18:00 and 20:00. Should i convert the times in arrays and subtract each item of an array one-by-one?

Link to comment
Share on other sites

Get the unix timestamps and subtract them, that will give the result in milliseconds.

var a = new Date();var b = new Date()a.setHour(18);b.setHour(20);milliseconds = b.UTC() - a.UTC();hours = milliseconds / (1000 * 60 * 60)

The other advantage that the Date object gives you is that you can tell the difference between hours that are on different days. For example, 23:00 one day and 4:00 the following day. If you just extracted the hours components of the strings you would get 4 - 23 which is -19. The real difference is 5 hours.

Link to comment
Share on other sites

I am talking about the case where you get the hour from a page. In this case the hour is a string. Your code example. i think, does not correspond to such a case. I am talking about the case where the user makes a selection of a time string in the page and this is passed into a variable. And this variable(which contains the time) is subtracted from an other variable(which also contains a time). What do you think?

Link to comment
Share on other sites

sounds exactly like what you need. why wouldn't what he gave you work? You can use parseInt to convert a string to a number, if that's what's throwing you off.

Link to comment
Share on other sites

You can use parse to convert a string to a date. The parse function actually returns a timestamp like Ingolme was referring to. So you can just parse your strings and subtract the two to get the difference (in milliseconds). You can then convert that to the appropriate measure of time.

Link to comment
Share on other sites

If you don't have date objects, you'll have to put all the data into one single unit and perform the operations with that unit. If the time is in the format "HH:mm:ss" then you'll have to split it into an array, multiply the first component by 3600, added to the second component multiplied by 60 and add that to the third component.Once you've done that for both dates, then perform the subtraction.

Link to comment
Share on other sites

If you don't have date objects, you'll have to put all the data into one single unit and perform the operations with that unit. If the time is in the format "HH:mm:ss" then you'll have to split it into an array, multiply the first component by 3600, added to the second component multiplied by 60 and add that to the third component.Once you've done that for both dates, then perform the subtraction.
Yea the time is on the format you are mentioning(without the seconds though),I will go with your method,that is convert it to an array first
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...