0

Having two different string times such as

var beginning = "22:30:00";
var end= "23:00:00";
var horaActual = DateTime.Now;

How can I know when current time is between them?

I am triying with this way:

    horaRango = DateTime.Now.Date.Add(TimeSpan.Parse(beginning));
    horaAgendada = DateTime.Now.Date.Add(TimeSpan.Parse(end));
                                
    if (horaActual >= horaRango && horaActual <= horaAgendamiento)
    {
      //is between
    }

But is not working pretty well

I am getting this error with my code:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'System.TimeSpan.Parse(string)' has some invalid arguments at CallSite.Target(Closure , CallSite , Type , Object )

Any idea?

devtester
  • 13
  • 3
  • How exactly is this not working for you? I just tried your code and it works. Though I did notice you use `horaAgendamiento` in the `if` but you set `horaAgendada`. Is that a typo or are you just using the wrong variable? – juharr Jul 03 '20 at 04:08
  • Complete answer is available in https://stackoverflow.com/questions/12998739/how-to-check-if-datetime-now-is-between-two-given-datetimes-for-time-part-only, if you have particular issues with your code please [edit] to clarify what you expect from *your* code and how it does not achieve that. – Alexei Levenkov Jul 03 '20 at 04:15

0 Answers0