简体   繁体   中英

convert string Time/Date values to dateTime value using vb.net

I have to controls(date picker,time picker) , I take the values as string, and I want to set them in this object, what can I do?

Note that .Date is declared as dateTime in the table.

dim  DateVal As String
dim TimeVal As String

 With Object

  .Date = ???

End With

You are "taking" them as string, how and more important, why? If you use a DateTimePicker control it has a Value property that returns a DateTime not a string.

So just use that property and don't convert or box the type to object and never convert it to String if you don't want to show it, leave it as what it is, a DateTime variable.

I am creating a reminder, the user has to to choose reminder date an time, I pass these values over a webservice method so I receive the values as string.

Then you're asking how to convert the string to DateTime , use Date.Parse or Date.TryParse :

If the format is common:

Dim dt As Date = Date.Parse(dateString)

If it might be invalid:

Dim dt As Date
If Date.TryParse(dateString, dt) Then
    ' ....
End If

If the format is uncommon or not the same as the current culture(read: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx ):

Dim format = "MMM dd, yyyy" ', e.g.: Oct 02, 2013
Dim dt As Date
If Date.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, dt) Then
     ' .... 
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM