c# - Get datetime from given day -


user enter day of week. instance user enter friday. need find exact date of given day , format dd.mm.yyyy. don't know how it.
example:

label1 - friday (entered user) label2 - 08.06.2012 (found system) 

label1 string (just friday). it's coming webservice string variable. need find date , compare today, if it's not equal or small today give date of upcoming friday, else give date of friday week after.

"if it's not equal or small today give exact date, else give next week date. "

assuming means return next date in future given day of week, exception when today given day of week.

public static datetime getnextweekdaysdate(string englweekdate) {     var desired = (dayofweek)enum.parse(typeof(dayofweek), englweekdate);     var current = datetime.today.dayofweek;     int c = (int)current;     int d = (int)desired;     int n = (7 - c + d);      return datetime.today.adddays((n >= 7) ? n % 7 : n); } 

let's test:

datetime monday   = getnextweekdaysdate("monday");    // 2012-06-11 datetime tuesday  = getnextweekdaysdate("tuesday");   // 2012-06-05  <-- !!! today datetime wednesday= getnextweekdaysdate("wednesday"); // 2012-06-06 datetime thursday = getnextweekdaysdate("thursday");  // 2012-06-07 datetime friday   = getnextweekdaysdate("friday");    // 2012-06-08 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -