Home > front end >  How to find the valid time if StartTime and EndTime is given?
How to find the valid time if StartTime and EndTime is given?

Time:12-29

How can the method be implemented in c#?

string StartTime = "06:10 PM";
string Endtime = "08:10 PM";
DateTime current_time = DateTime.Now;
 
bool validTime = validTimeFindout(StartTime,Endtime,current_time);

bool validTimeFindout(string StartTime, string Endtime,DateTime current_time){
//This method should return true 
when the current_time>= StartTime && current_time<=Endtime 
//otherwise false
}

I tried to find out the valid time in the specific range and for that validTimeFindout method will help and here the method is comparing the time get from local pc and compare them with StartTime and Endtime

CodePudding user response:

Starting with .NET 6, you can use the TimeOnly Struct:

static bool IsTimeBetween(string startTime, string endTime, DateTime dateTime)
{
    if (TimeOnly.TryParse(startTime, out var t1) &&
        TimeOnly.TryParse(endTime, out var t2))
    {
        return TimeOnly.FromDateTime(dateTime).IsBetween(t1, t2);
    }
    return false;
}

Note that TimeOnly.IsBetween supports time ranges that span midnight such as 23:00-01:00.

CodePudding user response:

You can use DateTime.TryParse() method to parse a string into the DateTime datatype. DateTime datatypes can be compared as numeric datatypes with <, <=, ==, !=, >=, >

bool validTimeFindout(string StartTime, string Endtime, DateTime current_time){
    DateTime start;
    DateTime.TryParse(StartTime, out start);
    DateTime end;
    DateTime.TryParse(Endtime, out end);
    return current_time >= start && current_time <= end;
}

CodePudding user response:

You just need to convert the inputs from string to datetime and do the calculations: datetime.parse()

    bool validTimeFindout(string StartTime, string Endtime, DateTime current_time)
    {
        DateTime _startTime = DateTime.Parse(StartTime);
        DateTime _endTime = DateTime.Parse(Endtime);
        if (current_time >= _startTime && current_time <= _endTime)
            return true;
        else
            return false;
    }

CodePudding user response:

First you need to convert those strings to DateTime objects. Then you should compare those DateTime objects and current time.

using System.Globalization;

string StartTime = "06:10 PM";
string Endtime = "08:10 PM";
DateTime current_time = DateTime.Now;

try {
   bool validTime = validTimeFindout(StartTime, Endtime, current_time);
   Console.WriteLine(validTime);
}catch(Exception exc) { 
    Console.WriteLine(exc.Message);
}

bool validTimeFindout(string StartTime, string Endtime, DateTime current_time) {
    DateTime start = ParseTimeString(StartTime);
    DateTime end = ParseTimeString(Endtime);
    if(current_time.CompareTo(start) >= 0 && current_time.CompareTo(end) <= 0) {
        return true;
    } else {
        return false;
    }
}

DateTime ParseTimeString(string timeString) {
    string format = "hh:mm tt";
    DateTime result;
    if (DateTime.TryParseExact(timeString, format, CultureInfo.GetCultureInfo("en-US"), DateTimeStyles.None, out result)) {
        return result;
    } else {
        throw new Exception("Cannot parse time!");
    }
}

There are also several things you can rethink and fix.

  1. Naming pattern - IMHO is valid is not a good name for method checking if a DateTime is between other DateTimes.
  2. Use single naming convention and stick to it. The most popular convention in C# is cammel case (like startTime).
  3. Is it really needed to store those DateTimes in strings formatted like "01:10 PM"? Why doesn't you simply store DateTime objects?
  • Related