Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a Timespan-Day last only for 8 Hours?

Tags:

c#

timespan

I have saved a duration in minutes and want to have an output "1 day 5 hours 30 minutes". Currently i add the minutes to a Timespan and do something like this:

TimeSpan ts = new TimeSpan(0,0,1800, 0);
Response.Write(ts.Days + "day(s) " + ts.Hours + " hour(s)" + ts.Minutes + " minutes");

But now i am only interested in the working-hours of a day. So when the duration of the TimeSpan is 27 Hours it should not create the output "1 Day 3 Hours". I want to have "3 Days 3 Hours".

Is there maybe an easy way to do that with the Timespan object? Is it possible to change the default behaviour of the TimeSpan? Or do i have to program my own custom Timespan-Class?

Thx cpt.oneeye

like image 671
cpt.oneeye Avatar asked Dec 02 '22 05:12

cpt.oneeye


2 Answers

Can you simply use:

(int)(ts.TotalHours / 8)

instead of ts.Days? Then use

(((int)ts.TotalHours) % 8)

instead of ts.Hours.

like image 171
BlueMonkMN Avatar answered Dec 05 '22 08:12

BlueMonkMN


You need to implement something like this:

TimeSpan workday = new TimeSpan(8, 0, 0);
int workdays = ts.Ticks / workday.Ticks
TimeSpan rest = new TimeSpan(ts.Ticks % workday.Ticks)
Response.Write(workdays  + "workday(s) and" + rest.ToString());

Will write something like

"3 workday(s) and 3:32"
like image 36
Stefan Steinegger Avatar answered Dec 05 '22 09:12

Stefan Steinegger