Check the time between two times?

I need to check whether the current time is between with todays date 9:30 next date 6:45 AM and get that shift person to be assigned to a ticket. When i am trying to do i am not getting the desired output i guess because of the date change, Can any one please help here?

date1 = Date.Today.AddHours(9).AddMinutes(30)
date2 = Date.Today.AddDays(1).AddHours(6).AddMinutes(45)

if(DateTime.Now > date1 and DateTime.Now < date2)


Hi Actually, we have to assign the tickets to person in night shift whose shift timings lies between 9:30PM(Say,Time 1) of 29th May to 6:45 AM of30th AM(Say, Time2). While comparing with the current time only in (HH:mm:ss) with the above two times it is failing to identify the shift. Even adding AddDays(1) to the second time and comparing with the Current time(IST). bot is failing, Can you please help me?

can you share your example?

Attached screenshots of the shift timings, we need to compare current time with shift timings and if the current time matches with say suppose S3 shift timings we have to get the S3.

As for example, Say now the time is 11AM- shifts come under this time is S1, G
Say now the time is 6PM- shifts come under this time is S2, G
Say now the time is 1AM- shifts come under this time is S3

All the shifts are working properly but only S3 is failing as i assume it involves in the day change

can you share your code and the error msg including the current state? Seems like your conversion is wrong

I didnt get any error, only the if condition is not satisfying and skipping the condition. Sorry somehow i am not able to attach the code.


for DateTimeS3Start( DateTime Var) = 01/01/2020 21:30:00
DateTimeS3End( DateTime Var) = 01/01/2020 06:45:00 ( Assigned values defaulty)
and for strISTTime = DateTime.ParseExact("01/01/2020 "+ strCurrentDate,“dd/MM/yyyy HH:mm:ss”,system.Globalization.CultureInfo.InvariantCulture) i used this condition and in strCurrentDate=TimeZoneInfo.ConvertTime(DateTime.Now,TimeZoneInfo.FindSystemTimeZoneById(“Pacific Standard Time”),TimeZoneInfo.FindSystemTimeZoneById(“India Standard Time”))


you have to add one day because DateTimeS3End is before DateTimeS3Start

I have done that but still condition is failing.

Sorry let me check that

Even after adding 1day if condition is still not passing , it is skipping and going to Else part

can you share the current state of each var before executing the if activity

strCurrentDate : “01:30:15”
DateTimeS3Start: [01/01/2020 15:15:00]
DateTimeS3End : [02/01/2020 06:45:00]
StrISTTime : [01/01/2020 01:30:15]

Condition : strISTTime.TimeOfDay>=DateTimeS3Start.TimeOfDay And strISTTime.TimeOfDay <DateTimeS3End.AddDays(1).TimeOfDay
After Executing(In String) : 01:30:15>=21:30:00 And 01:30:15 < 06:45:00

I see the problem. dont use TimeOfDay =>
TimeOfDay compares the time, you want compare day and time

strISTTime>=DateTimeS3Start And strISTTime<DateTimeS3End.AddDays(1)

Hi @Madhups ,

Create two variable like below:

If Now.hour >=0 and Now.hour <=6
ShiftStart = new DateTime(Today.Year,Today.Month,Today.Day -1,21,30,00)
ShiftEnd = new DateTime(Today.Year,Today.Month,Today.Day,06,45,00)
ShiftStart = new DateTime(Today.Year,Today.Month,Today.Day,21,30,00)
ShiftEnd = new DateTime(Today.Year,Today.Month,Today.Day+1,06,45,00)

then you can simply do a
Now > ShiftStart and Now < ShiftEnd.

@Catfish1115 Thank you for the help it is working but with slight changes strISTTime>=DateTimeS3Start .AddDay(-1) And strISTTime<DateTimeS3End.

Above code worked fine for me

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.