KyLim
KyLim

Reputation: 476

c# check date is weekend or weekday

DayOfWeek today = DateTime.Today.DayOfWeek;
if (today == DayOfWeek.Sunday || today == DayOfWeek.Saturday)
{
  ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" weekend"');", true);
}
else
{
    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" weekday"');", true);
}

From line above i am able to find if today is weekend or weekday

now i want to get the date from user input i tried this but fail,

my text input format : 2016-10-04

string dateInput = dateTextbox.Text;
DayOfWeek today = DateTime.dateInput.DayOfWeek;

Upvotes: 0

Views: 4827

Answers (2)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

IMO, you should use some DateTime control instead of a TextBox to enter DateTime. till then you need to convert TextBox Text to DateTime object first.

DateTime dt = DateTime.ParseExact(dateTextbox.Text, 
                                  "YYYY-MM-DD", 
                                  CultureInfo.InvariantCulture);

FYI, there are numerous ways to convert String to DateTime (google will help). All have their pros and cons. Use which best suits you.

Upvotes: 2

fubo
fubo

Reputation: 45947

Use DateTime.ParseExact to parse your string into a DateTime

string dateInput = dateTextbox.Text; //"2016-10-04"
DateTime dtResult = DateTime.ParseExact(dateInput, "yyyy-MM-dd", CultureInfo.InvariantCulture);
DayOfWeek today = dtResult.DayOfWeek;

Upvotes: 4

Related Questions