CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2008
    Posts
    90

    [RESOLVED] Problem in setting DateTime value explicitly.

    I want to pick a value from a DateTimePicker in 'MM/dd/yyyy hh:mm tt' format, add 00 as second with it, store it in a DateTime variable and save it to database. Suppose if I pick the value 05/28/2010 9:25 AM or 05/28/2010 9:25:34 AM from the DateTimePicker, it'll be stored in the variable and saved to database as 05/28/2010 9:25:00 AM irrespective of what value is chosen from the DateTimePicker for second. I'm using the following statement but it's getting failed during the format conversion while storing it in the DateTime variable generating an error.

    Dim dtmReminderTime As DateTime
    dtmReminderTime = DateTime.Parse(Format(dtpReminderTime.Value, "MM/dd/yyyy hh:mm") + ":00 " + Format(dtpReminderTime.Value, "tt"))

    I tried the other way but failed:
    dtmReminderTime = Convert.ToDateTime(Format(dtpReminderTime.Value, "MM/dd/yyyy hh:mm") + ":00 " + Format(dtpReminderTime.Value, "tt"))

    I have also tried using culture specific information but of no avail. Please help.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Problem in setting DateTime value explicitly.

    Try this:

    Code:
        Dim dtmReminderTime As String
        dtmReminderTime = String.Format(dtpReminderTime,  "MM/dd/yyyy hh:mm:00 tt")
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Sep 2008
    Posts
    90

    Re: Problem in setting DateTime value explicitly.

    No I can't change the datatype of the variable to string. This is a good option:
    Dim dtmReminderTime As DateTime
    dtmReminderTime = dtpReminderTime.Value.AddSeconds(-dtpReminderTime.Value.Second)

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Problem in setting DateTime value explicitly.

    priyamtheone, please use [CODE] tags when posting code.

    I have asked this before, and this is the last time I'll ask.

    It is explained here :

    http://www.codeguru.com/forum/showthread.php?t=403073

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

Featured