-2

The TimeSpan default day considers 24hrs as 1 day. I want to have 8 hrs as 1 day. So that If I give 1 day it should consider it as 8 hrs not 24 hrs. And if I give 23 hrs it should consider it as 2 days and 7 hrs.

`

Task T1 = project.AddTask("T1");
Task T2 = project.AddTask("T2");
T1.Start = new DateTime(2015, 3, 9);
T1.Duration = new TimeSpan(1, 2, 0, 0); 
T2.Duration =new TimeSpan(0, 23, 0, 0)
Console.WriteLine(T1.Name + "Duration: " + T1.Duration)://Considers 1 day as 24 hrs;

` Is there any way to do this?

Nishanth Bhat
  • 56
  • 1
  • 7
  • 1
    There's nothing in the docs (https://msdn.microsoft.com/en-us/library/system.timespan%28v=vs.110%29.aspx) to suggest this is possible natively. You'd have to make arithmetic on the Timespan result –  Mar 06 '15 at 10:15
  • 1
    Something like `timeSpan.TotalHours / 8`. – Frédéric Hamidi Mar 06 '15 at 10:15
  • 3
    Take a look at this: http://stackoverflow.com/questions/3492958/how-can-a-timespan-day-last-only-for-8-hours – eren Mar 06 '15 at 10:16
  • I imagine you'd have to make a custom `TimeSpan`-like object and use that instead of the built-in one. I doubt the .NET team ever considered that the length of a day would need to drastically change. – David Mar 06 '15 at 10:16
  • I want to set the value to the task's (T1) duration. – Nishanth Bhat Mar 06 '15 at 10:21
  • @FrédéricHamidi wouldn't that make each day count as 3? –  Mar 06 '15 at 10:21
  • Try to create a custom class like Date Time which considers 8 hours as a day. In this way you can customize its behavior as what ever you wanted. Just give a try if u have time – Dhanasekar Murugesan Mar 06 '15 at 10:22
  • the TimeSpan.Hours property is read only. So I can't assign it some value – Nishanth Bhat Mar 06 '15 at 10:22
  • @Sphaso, yup, that's the goal. As the questioner says, *if I give 23 hrs it should consider it as 2 days and 7 hrs*. So 24 hours => 3 "days". – Frédéric Hamidi Mar 06 '15 at 10:24
  • @FrédéricHamidi oh my bad, I thought he wanted to count each day as only having 8 hours. –  Mar 06 '15 at 10:25
  • 1
    @Sphaso, well, that's pretty much the same thing :) – Frédéric Hamidi Mar 06 '15 at 10:26
  • @FrédéricHamidi I figured he wanted to make it so that when timespan gives you 24h you only count 8. I obviously misread the question. –  Mar 06 '15 at 10:27

1 Answers1

0

If you are trying to compute 'working days' and overtime there are numerous problems you will want to consider. I have done this before and I suggest NOT using the timespan object

however a trivial solution is ;

    public class Task
    {
        public DateTime Start;
        public TimeSpan Duration;

        public TimeSpan GetWorkingDays()
        {
            int days = (int)Math.Floor(this.Duration.TotalHours/8);
            int hours = (int)Math.Floor(this.Duration.TotalHours - days * 8);

            return new TimeSpan(days, hours, 0, 0);
        }
    }
Ewan
  • 1,261
  • 1
  • 14
  • 25