Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Operator '==' cannot be applied to operands of type 'DateTime' and 'string'

In c#, I am trying to compare the current day with the day the user has chosen from the checkboxes via the website. CDays contains the list Mon-Fri (Name) and Selected will be true if the user has checked the current days’ checkbox. For example today is Friday, if the user has checkboxed Friday on the website return true.

I am running into Operator ‘==’ cannot be applied to operands of type ‘DateTime’ and ‘string’ and I’m not sure how to convert DateTime to a string

 public List<CDay> days { get; set; }
    public CDay()
    {
     Name = string.Empty; // Monday, Tuesday, Wednesday, Thursday, Friday
     Selected = false; //will be true if user has selected this checkbox
    }
    

my logic:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private bool currentDayOfTheWeek (DateTime now, List <CDay> days)
        {
          now = DateTime.Now;
        
          for (int i = 0; i < days.Count; i++)
          {

            if ( days[i].Selected.Equals(true) && now == days[i].Name) 
                 {
                     return true;
                 }
              }
            return false;
          }

>Solution :

Please use now.DayOfWeek.ToString() == days[i].Name

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading