Problem in setting DateTime value explicitly.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • priyamtheone
    New Member
    • Sep 2007
    • 88

    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(dtpRemin derTime.Value, "MM/dd/yyyy hh:mm") + ":00 " + Format(dtpRemin derTime.Value, "tt"))

    I tried the other way but failed:
    dtmReminderTime = Convert.ToDateT ime(Format(dtpR eminderTime.Val ue, "MM/dd/yyyy hh:mm") + ":00 " + Format(dtpRemin derTime.Value, "tt"))

    I have also tried using culture specific information but of no avail. Please help.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Don't try parsing it... That's just a mess. Keep it as a DateTime object.
    Get the DateTime.second s property. Then add a negative number of seconds to it. That will be :00

    Suedocode - you'll have to make the VB version of this.

    myDateTimeVaria ble = 05/may/2010 at 14:50:23
    myDateTimeVaria ble.AddSeconds(-1 * myDateTimeVaria ble.Seconds);

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Be sure to note that AddSeconds() (and similar functions) don't modify the active datetime object, but rather return a new one with the changes applied to it

      Comment

      • priyamtheone
        New Member
        • Sep 2007
        • 88

        #4
        Originally posted by tlhintoq
        Don't try parsing it... That's just a mess. Keep it as a DateTime object.
        Get the DateTime.second s property. Then add a negative number of seconds to it. That will be :00

        Suedocode - you'll have to make the VB version of this.

        myDateTimeVaria ble = 05/may/2010 at 14:50:23
        myDateTimeVaria ble.AddSeconds(-1 * myDateTimeVaria ble.Seconds);
        Optimum solution. Thanks.

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Be sure to note that AddSeconds() (and similar functions) don't modify the active datetime object, but rather return a new one with the changes applied to it
          Very very true. I've spent hours chasing my tail not realizing why my code isn't working, only to later realize I was calling a method that returns its result rather than applying the result to the object that called it.

          For the sake of completeness and to help anyone that comes across this post later, here is a corrected version of tlhintoq's example:

          Code:
          myDateTimeVariable = 05/may/2010 at 14:50:23
          myDateTimeVariable = myDateTimeVariable.AddSeconds(-1 * myDateTimeVariable.Seconds);

          Comment

          Working...