Click to See Complete Forum and Search --> : [RESOLVED] Problem in setting DateTime value explicitly.


priyamtheone
May 7th, 2010, 11:19 AM
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.

dglienna
May 7th, 2010, 06:12 PM
Try this:

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

priyamtheone
May 11th, 2010, 11:22 AM
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)

HanneSThEGreaT
May 12th, 2010, 07:16 AM
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