Results 1 to 2 of 2

Thread: day of week and time to actual date

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2011
    Posts
    132

    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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Function GetNextDayOfWeekDate(dayOfWeek As String) As Date
    2.     Return GetNextDayOfWeekDate(DirectCast([Enum].Parse(GetType(DayOfWeek),
    3.                                                         dayOfWeek),
    4.                                 DayOfWeek))
    5. End Function
    6.  
    7. Private Function GetNextDayOfWeekDate(dayOfWeek As DayOfWeek) As Date
    8.     Dim d = Date.Today
    9.  
    10.     Do While d.DayOfWeek <> dayOfWeek
    11.         d = d.AddDays(1)
    12.     Loop
    13.  
    14.     Return d
    15. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width