0
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;
KyLim
  • 468
  • 1
  • 6
  • 22

2 Answers2

4

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;
fubo
  • 44,811
  • 17
  • 103
  • 137
  • ,hi how to check the input is weekday or weekend? – KyLim Nov 08 '16 at 08:48
  • as you mentioned `if (today == DayOfWeek.Sunday || today == DayOfWeek.Saturday)` – fubo Nov 08 '16 at 08:48
  • @KyLim I don't know if you have 24h or 12h format. for 24 use `HH`, for 12 `hh` – fubo Nov 08 '16 at 08:51
  • hye master fubo, getting error in this line DateTime dtResult = DateTime.ParseExact(dateInput, "yyyy-MM-dd hh:mm:ss.fff", CultureInfo.InvariantCulture); error message : System.FormatException: String was not recognized as a valid DateTime. – KyLim Nov 08 '16 at 08:56
  • 2016-11-08 ,this is the content i get – KyLim Nov 08 '16 at 09:03
2

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.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • (today == DayOfWeek.Sunday || today == DayOfWeek.Saturday) cannot compare against dayofweek and datetime – KyLim Nov 08 '16 at 08:29
  • 1
    if(dt.DayOfWeek == DayOfWeek.Sunday || dt.DayOfWeek == DayOfWeek.Saturday) – Nikhil Agrawal Nov 08 '16 at 08:30
  • @KyLim: So you need to use the `DayOfWeek` property once you've converted it into a `DateTime`. You clearly know about the `DayOfWeek` property, because you're using it in the question. Software engineering is all about breaking a "big" problem into lots of tiny steps. – Jon Skeet Nov 08 '16 at 08:30
  • @KyLim: Use a DateTime Control. – Nikhil Agrawal Nov 08 '16 at 08:33
  • @NikhilAgrawal DateTime Control able check date is weekday or weekend? – KyLim Nov 08 '16 at 08:34
  • this answer is too vague since you don't know if `DateTime.Parse` does work - and _google for your problem on your own_ could also be a comment – Impostor Nov 08 '16 at 08:39
  • @KyLim: No. With TextBox, 20 users can put date in 20 diff formats, with DTControl you can eliminate that issue. – Nikhil Agrawal Nov 08 '16 at 08:42
  • $('.datepicker').daterangepicker({ "singleDatePicker": true, "showDropdowns": true, "autoApply": true, locale: { format: 'YYYY-MM-DD' } }); user input is same format – KyLim Nov 08 '16 at 08:43