2

first of all i checked this solution:

Find if current time falls in a time range

and tried to compare timespans like that solution

public Boolean CalculateDalUren(DateTime datum, TimeSpan? dalStart, TimeSpan? dalEnd)
    {
        Boolean isDal = false;
        TimeSpan timeBetween = datum.TimeOfDay;

        if ((timeBetween >= dalStart))&&(timeBetween < dalEnd)
            {
                isDal = true;
            }
        }
        return isDal;
    }

Note that dalStart is either 21:00 or 23:00 and dalEnd is pretty much always 07:00. And I convert the DateTime to a Timespan.

Now if a Timespan for example is 23:00 then the time is bigger or the same as dalStart but because (and this is an assumption) it is later then dalEnd it will still see the if statement as false. vice versa when its 02:00 hours. Then its not later then dalStart but earlier then dalEnd.

I think this is because my timespan covers 2 days. 21:00 hours from one day, to 07:00 hours the next day. Is there a workaround for this? So i can check if a time is between 21:00 and 07:00 the next morning.

Community
  • 1
  • 1
Proliges
  • 371
  • 4
  • 26
  • A timespan is not bound to any point in time, it's simply a description of a *duration of time*. What exactly are you trying to accomplish? – Mathias R. Jessen Aug 03 '16 at 10:32
  • TImeSpan != Time, so 21:00 means 21 hours, not 9.p.m. – Wolfgang Aug 03 '16 at 10:33
  • yes and at 00:00 time = 0 and thus i cannot compare anymore. Thats the point i guess. @MathiasR.Jessen I want the `boolean IsDal` to be true when the time in the given `DateTime` is between the times i give in the 2 `TimeSpan` which are most of the time 21:00 and 07:00 hours. So if the time in `DateTime` = 02:00 it should return true, if its 14:00 hours false. – Proliges Aug 03 '16 at 10:40

2 Answers2

5

I think this matches your requirement.

If dalEnd is lesser than dalStart, It should be the TimeSpan of the nextday.

    public Boolean CalculateDalUren(DateTime datum, TimeSpan? dalStart, TimeSpan? dalEnd)
    {
        Boolean isDal = false;
        DateTime StartDate = DateTime.Today; 
        DateTime EndDate = DateTime.Today;

        //Check whether the dalEnd is lesser than dalStart
        if (dalStart >= dalEnd)
        {
            //Increase the date if dalEnd is timespan of the Nextday 
            EndDate = EndDate.AddDays(1); 
        }

        //Assign the dalStart and dalEnd to the Dates
        StartDate = StartDate.Date + dalStart.Value; 
        EndDate = EndDate.Date + dalEnd.Value;

        if ((datum >= StartDate) && (datum <= EndDate))
        {
            isDal = true;
        }
        return isDal;
    }

try it.

  • 1
    Converting the `TimeSpan` to `Datetime` and increasing the day `if (timeBetween.TimeOfDay < endDate.TimeOfDay) { timeBetween = timeBetween.AddDays(1); }` is the correct answer. Thanks for your help – Proliges Aug 04 '16 at 09:35
  • I thought dalStart and dalEnd timespan variables are fixed and so i made it that way. It looks nice now. Welcome Proliges. –  Aug 04 '16 at 10:56
0

Create two dates which is the 00:00 of the actual day, add the dalStart, dalEnd timespan to them and if ( datum > datetimeFromDalStart and datum < dateTimeFromDalEnd ) ....

Dexion
  • 1,101
  • 8
  • 14