简体   繁体   中英

C# Cannot implicitly convert type 'string' to 'System.DateTime'

DateTime Time = DateTime.Now;
string format = "ddd";
Console.WriteLine(Time.ToString(format));
if (Time = "Mon; Tue; Wed; Thu; Fri;") 

{
    Console.WriteLine("It is a week day");
}

else
{
    Console.WriteLine("it is a weekend");
}

I'm trying to get this code to check for the date and the display a response based on the day. Sorry if this is a simple question but I'm new to coding and I cant find a solution anywhere

You can do it like this.

bool isWeekDay == DateTime.Today.DayOfWeek > DayOfWeek.Sunday 
    && DateTime.Today.DayOfWeek < DayOfWeek.Saturday;

if (isWeekDay) {
    Console.WriteLine("It is a week day");
}
else {
    Console.WriteLine("it is a weekend");
}

try

DateTime Time = DateTime.Now;
string format = "ddd";
Console.WriteLine(Time.ToString(format));
string day = Time.ToString(format);
if (day = "Mon; Tue; Wed; Thu; Fri;") 
{
    Console.WriteLine("It is a week day");
}
else
{
    Console.WriteLine("it is a weekend");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM