Saksham Srivastava
Saksham Srivastava

Reputation: 37

How to Check whether the time is ending on the same day or the next day?

My time is currently like the below format

ShiftDate StartTime EndTime
01-04-2022 03:50 08:02
01-04-2022 08:02 09:02
01-04-2022 09:02 12:47
01-04-2022 13:00 17:12
01-04-2022 17:12 18:12
01-04-2022 18:12 01:12

I have two fields in my API request :

StartDayNumber and EndDayNumber

For Example : In my last record in the table, start time is 18:12 and end time 01:12, so end time is ending on the second day. In this case my EndDayNumber value will increase by 1. Suppose StartDayNumber is 1 then EndaDaynumber should become 2.

I need to know how to calculate this endDayNumber by checking whether the end time is ending on the same day or the next day.

Currenlty my code looks like this :-


    int startDayNumber = 1;
    int endDayNumber = 1;
    
    DateTime sdt = DateTime.Parse(item.StartDatetime);
    DateTime edt = DateTime.Parse(item.EndDatetime);
    TimeSpan StartTime = DateTime.ParseExact(sdt.ToString(), "h:mm:ss tt", CultureInfo.InvariantCulture).TimeOfDay;
    TimeSpan EndTime = DateTime.ParseExact(edt.ToString(), "h:mm:ss tt", CultureInfo.InvariantCulture).TimeOfDay;
    bool IsNextDay = EndTime < StartTime; //false
    
    //case 2
    TimeSpan StartTime2 = DateTime.ParseExact(sdt.ToString(), "h:mm:ss tt", CultureInfo.InvariantCulture).TimeOfDay;
    TimeSpan EndTime2 = DateTime.ParseExact(edt.ToString(), "h:mm:ss tt", CultureInfo.InvariantCulture).TimeOfDay;
    bool IsNextDay2 = EndTime2 < StartTime2; //true
    
    if (IsNextDay2 == true)
    {
    endDayNumber = startDayNumber + 1;
    }

The item.StartDateTime field is startTime in the table and item.EndDateTime field is endTime in the table.

I am getting error String '2022-08-03 03:50:00' was not recognized as a valid DateTime,the time is not in correct format. Please help me finding a solution to this.

Note : The time has no AM/PM and is in 24 hour format.

Upvotes: 1

Views: 182

Answers (2)

Rand Random
Rand Random

Reputation: 7440

To check if a DateTime object is in a another day use this:

DateTime dateTimeStart = DateTime.ParseExact("01-04-2022 18:12", "dd-MM-yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture);
DateTime dateTimeEnd = DateTime.ParseExact("02-04-2022 01:12", "dd-MM-yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture);

DateTime dateStartDay = dateTimeStart.Date;
DateTime dateEndDay = dateTimeEnd.Date;

TimeSpan timeSpanDifference = dateEndDay - dateStartDay;
Console.WriteLine(timeSpanDifference.TotalDays);

https://dotnetfiddle.net/4Db7c0


Assuming you have the following information

ShiftDate StartTime EndTime
01-04-2022 18:12 01:12

Exactly as shown, as a string object you can do this, to get the correct DateTime objects.

Yes, I am assuming the format being dd-MM-yyyy if it is MM-dd-yyyy change it accordingly.

Though I am doubting the format, since you said you are getting this exception I am getting error String '2022-08-03 03:50:00' was not recognized, so based on the exception the format could be either yyyy-MM-dd or yyyy-dd-MM, again just need to change it to what ever format it actually is.

var shiftDateString = "01-04-2022";
var startTimeString = "18:12";
var endTimeString = "01:12";

var shiftDateTime = DateTime.ParseExact(shiftDateString, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
shiftDateTime = shiftDateTime.Date;

var startDateTime = DateTime.ParseExact(startTimeString, "HH:mm", System.Globalization.CultureInfo.InvariantCulture);
var endDateTime = DateTime.ParseExact(endTimeString, "HH:mm", System.Globalization.CultureInfo.InvariantCulture);

startDateTime = shiftDateTime.Add(startDateTime.TimeOfDay);
endDateTime = shiftDateTime.Add(endDateTime.TimeOfDay);

if (endDateTime < startDateTime)
    endDateTime = endDateTime.AddDays(1);

//output
Console.WriteLine(shiftDateTime.ToString("dd-MM-yyyy HH:mm"));
Console.WriteLine(startDateTime.ToString("dd-MM-yyyy HH:mm"));
Console.WriteLine(endDateTime.ToString("dd-MM-yyyy HH:mm"));

TimeSpan timeSpanDifference = endDateTime.Date - startDateTime.Date;
Console.WriteLine(timeSpanDifference.TotalDays);

https://dotnetfiddle.net/9VR4xE

Upvotes: 3

Palle Due
Palle Due

Reputation: 6292

Here is a very simple helper class that you can use. It overrides the < and > operators, so you can use it to determine which time is greater. You would need to implement some error handling:

public class JustTime
{
    public int Hours {get;set;}
    public int Minutes {get;set;}
    
    public JustTime(string s)
    {
        string[] parts = s.Split(':');
        Hours = Int32.Parse(parts[0]);
        Minutes = Int32.Parse(parts[1]);
    }
    
    public static bool operator < (JustTime l, JustTime r)
    {
        if (l.Hours < r.Hours)
        {
            return true;
        }
        if (l.Hours == r.Hours)
        {
            return l.Minutes < r.Minutes;
        }
        return false;
    }
    
    public static bool operator > (JustTime l, JustTime r)
    {
        if (l.Hours > r.Hours)
        {
            return true;
        }
        if (l.Hours == r.Hours)
        {
            return l.Minutes > r.Minutes;
        }
        return false;
    }
}

Then you can just do:

int startDayNumber = 1;
int endDayNumber = 1;
    
var firstTime = new JustTime(item.StartDatetime);
var secondTime = new JustTime(item.EndDatetime);

if (firstTime > secondTime)
{
    endDayNumber = startDayNumber + 1;
}

If firstTime and secondTime are equal, you need to find out whether you consider it an empty interval or a full day.

Upvotes: 1

Related Questions