Hi;
How do I assign a starting date and time to a DateTimePicker. I tried...
myPicker.text = "01/26/09 08:00" but the following error message is returned....
String was not recognized as a valid DateTime
What am I doing wrong?
Printable View
Hi;
How do I assign a starting date and time to a DateTimePicker. I tried...
myPicker.text = "01/26/09 08:00" but the following error message is returned....
String was not recognized as a valid DateTime
What am I doing wrong?
Set the Value property like this:
VB.NET Code:
myPicker.Value = New Date(2009, 1, 26, 8, 0, 0)
Setting the text property like you're trying to do is equivalent to doing this:
VB.NET Code:
myPicker.Value = DateTime.Parse("01/26/09 08:00", CultureInfo.CurrentCulture)
And the error you're getting would be because it was unable to parse your string as a proper DateTime.
Code:Dim s As String = "01/26/09 08:00"
If DateTime.TryParse(s, myPicker.Value) Then
'string was a valid datetime format
Debug.WriteLine("OK " & myPicker.Value.ToString)
Else
'bad datetime
Debug.WriteLine("BAD " & myPicker.Value.ToString)
End If
***UNRELATED TO QUERY***
In answer to the age old question, "Where's the peck of pickled peppers Peter Piper picked?"
Peter Piper put the peck of pickled peppers in his pink and purple polyester pants pocket.
Here's another way. You can create a date object by wrapping your date in pound signs rather than quotes.
Code:myPicker.Value = #1/26/2009 8:00:00 AM#
This should work also.
Code:myPicker.Value = Convert.ToDateTime("01/26/2009 8:00:00 AM")
Why would you assign a literal in code anyway? If you want to assign a literal to the Value property of a DateTimePicker then would you not do so in the designer? If you need to do some calculation to get the value then you'd be using a DateTime value anyway, so this whole discussion becomes moot.
dee-u
Your suggestion was about the easiest to implement however to see the result I have to add an extra line...
myPicker.focus
Any idea why this is so?
jmcilhinney
Why would you assign a literal in code anyway? I wouldn't, the code sample was to illustrate what I need to do.
I am retrieving a time from a database table and use this as an arbitrary start point for a DateTimePicker. The value showing in the DateTimePicker is today's date combined with the time value from the table.
i vote for my idea if the datetime value changes. if it is a constant then i vote for jmc. "easiest to implement" scares me.
btw - don't use focus. if you need to give focus to a control use .Select
Code:Dim s As String = "7/29/09 08:00"
myPicker.Format = DateTimePickerFormat.Custom
myPicker.CustomFormat = "MM/dd/yyyy hh:mm:ss tt"
If DateTime.TryParse(s, myPicker.Value) Then
'string was a valid datetime format
Debug.WriteLine("OK " & myPicker.Value.ToString)
Else
'bad datetime
Debug.WriteLine("BAD " & myPicker.Value.ToString)
End If
myPicker.Select()
If you are recieving the date from a database then I assume the database field is of type DateTime (if its SQL or similar anyway). In which case then you could just set the value of the datetimerpicker to the value you got from the database directly by using the DateTime type like JMC said.
A very simple example (where GetStartDate is a function that returns a DateTime type object which you got from the databse):
vb.net Code:
Datetimepicker1.Value = GetStartDate()
EDIT: here's an example of how the GetStartDate function might look just to clarify what I mean:
Obviously thats very basic and assumes a few things (and its written from memory so might need some small corrections to run properly) but you get the ideavb Code:
Private Function GetStartDate As DateTime Dim SelectCommand As New SqlCommand("SELECT StartDate FROM SomeTableName WHERE RowID=1",MySqlConnection) Dim Result As DateTime MySqlConnection.Open Result = SelectCommand.ExecuteScalar() MySqlConnection.Close Return Result End Function
Thanks guys for all the help. Your suggestions all work well but I have discovered that these two statements are required.
DTP_Start1.Format = DateTimePickerFormat.Custom
DTP_Start1.CustomFormat = "MM/dd/yy HH:mm"
I'm not sure why because I set them when I created the controls. However my control, DTP_Start1, will not display the date/time without them.
So here is what dee-u's code looks like in the working state
DTP_Start1.Format = DateTimePickerFormat.Custom
DTP_Start1.CustomFormat = "MM/dd/yy HH:mm"
DTP_Start1.Value = Convert.ToDateTime(Format(Now(), "MM/dd/yy") & " " & Format(myDbCommand.Parameters(3).Value, "HH:mm"))
dbasnett's code
s = Format(Now(), "MM/dd/yy") & " " & Format(myDbCommand.Parameters(3).Value, "HH:mm")
DTP_Start1.Format = DateTimePickerFormat.Custom
DTP_Start1.CustomFormat = "MM/dd/yy HH:mm"
DateTime.TryParse(s, DTP_Start1.Value)
What does myPicker.select() do?
I think JMC was curious about why I was coding a literal string and I think my explanation cleared it up.
Thanks for sharing your code chris128
Control.Focus would focus on the control, why are you suggesting .Select? :confused:Quote:
Originally Posted by dbasnett
You're looking in the ".NET Framework Class Library for Silverlight"-section of MSDN ;) Here is the windows forms version of the Control.Focus documentation, which states:Quote:
Originally Posted by dee-u
Quote:
Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
Aahhh, I see. I am exclusively using Focus without problems but knowing about Select is nice. I wonder how one could demonstrate any repercussions on using Focus instead of Select.Quote:
Originally Posted by Atheist
Yes, your explanation does clear it up. It shows us exactly what you're doing wrong. If you have two DateTime values to start with and you want a DateTime value at the end then there is no reason or point to using any strings at all:That will take today's date and add the time portion of another DateTime, then assign the result to a third DateTime, which is exactly what you want to do. NEVER use strings to represent dates and times unless it is for display or serialisation purposes.Code:date2 = Date.Today.Add(date1.TimeOfDay)
Hi jmcilhinney
I coded your suggestion last night and it works great. Thanks for taking to the time and effort to help with this.
Everyone's suggestions and advice are most appreciated.
Lin