day of week and time to actual date
lets say i have a string that holds "Monday" and another string that holds "9:45:00 PM"
would it be possible to look at the Date.Now() function and see when the next Monday is...and then convert those two strings into a date?
lets say the next monday was 05/20/2011
I would want the date variable to hold "05/20/2011 9:45:00 PM"
any ideas guys?
Re: day of week and time to actual date
Date.Now is actually a property, not a function. Also, where exactly is this data coming from in the first place? Most likely, there's a better way to get it such that it won't be in String form to begin with.
Anyway:
vb.net Code:
Private Function GetNextDayOfWeekDate(dayOfWeek As String) As Date
Return GetNextDayOfWeekDate(DirectCast([Enum].Parse(GetType(DayOfWeek),
dayOfWeek),
DayOfWeek))
End Function
Private Function GetNextDayOfWeekDate(dayOfWeek As DayOfWeek) As Date
Dim d = Date.Today
Do While d.DayOfWeek <> dayOfWeek
d = d.AddDays(1)
Loop
Return d
End Function
You can use the Parse, TryParse, ParseExact or TryParseExact method of the TimeSpan structure to convert your time string to a TimeSpan, which you can then add to your Date.