locked
How to compare HH:MM in C# RRS feed

  • Question

  • Hi I have to compare HH:MM(hour and minutes). How can i do so?

      String hourMinute;
                hourMinute = DateTime.Now.ToString("HH:mm");
                if (hourMinute >= '12:15' && hourMinute <= '12:30')
                {
                }

    Monday, June 19, 2017 10:20 AM

Answers

  • You can use TimeSpan

    TimeSpan hourMinute;
    hourMinute = DateTime.Now.TimeOfDay;
    if (hourMinute >= new TimeSpan(12, 15, 0) && hourMinute <= new TimeSpan(12, 30, 0)) {
    			
    }



    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    • Marked as answer by YassirCool Monday, June 19, 2017 3:52 PM
    Monday, June 19, 2017 10:33 AM

All replies

  • Try this way

    DateTime hourMinute;
    hourMinute = DateTime.Now;

    if (hourMinute.Hour >= 12 && hourMinute.Minute >= 15) && (hourMinute.Hour <= 12 && hourMinute.Hour <= 30))
    {
     MessageBox.Show("Time between 12:15 asd 12:30");
    }

    hope this help you.

    Regards.






    Monday, June 19, 2017 10:32 AM
  • You can use TimeSpan

    TimeSpan hourMinute;
    hourMinute = DateTime.Now.TimeOfDay;
    if (hourMinute >= new TimeSpan(12, 15, 0) && hourMinute <= new TimeSpan(12, 30, 0)) {
    			
    }



    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    • Marked as answer by YassirCool Monday, June 19, 2017 3:52 PM
    Monday, June 19, 2017 10:33 AM