I would like to parse date: "wednesday 02 2000"
so the DateTime
object I get will be set to the first wednesday of February 2000. Unfortunately the ParseExact
method doesn't work that way. Is this is possible to achieve in C# by an existing method?
String s1 = "tuesday 02 2000";
String s2 = "wednesday 02 2000";
String format = "dddd MM yyyy";
Console.WriteLine(DateTime.ParseExact(s1, format, new CultureInfo("en-US")));//doesn't throw an exception because tuesday was 01.02.2000
Console.WriteLine(DateTime.ParseExact(s2, format, null));//throws an exception because wednsday was 02.02.2000
I know solution for this problem: setting DateTime to the 1.02.2000 and then adding days until i reach wednesday, but I am not happy with that soluton.
while (date.DayOfWeek != day) {
date = date.AddDays(1);
}