Anyname Donotcare
Anyname Donotcare

Reputation: 11403

How to substract and round times

Q:

If i have two periods in the following format .ToString("H:m"); and i wanna to firstly subtract the end period from the first period ,then round the result .like the following example:

13:00 ---->First period.

13.45 ---->Last period.

the result 45 ----> round to 60

if the result = 75 for example then round to 120

Upvotes: 2

Views: 153

Answers (2)

Amar Palsapure
Amar Palsapure

Reputation: 9680

Something like this

 var t1 = TimeSpan.Parse("13:00").TotalMinutes;
 var t2 = TimeSpan.Parse("13:45").TotalMinutes;
 var round = (1 + ((int)(t2 - t1) / 60)) * 60; //Assuming t2 is always greater than t1

Hope this is what your looking for.

Upvotes: 3

Just Me
Just Me

Reputation: 244

You can use datetime.parse to get two datetime objects for first and last period then subtract them using lastperiod.subtract(firstperiod) this will return u a timespan object you can then round it as you want

Upvotes: 0

Related Questions