Вопрос

In the form I use DateTimePicker like this:

With dtp_myDate
    .Format = DateTimePickerFormat.Custom
    .CustomFormat = "dd.MM.yyyy."
    .Value = CType("16.12.2013. 11:30:25.1234", Date)
End With

Also I may use CDate instead of CType where in both cases date is expressed as string with date and time with milliseconds.
I can't change this since data comes from (not mine) database.

On machine with windows 7 that work OK but on windows XP machine I get error exception with message that this string is not valid for Date conversion.

Can this be be solved that such code work on both machines without error and how?

Это было полезно?

Решение

You want to use DateTime.ParseExact

An example is provided here: http://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx

Changing this to your needs as follows:

    Dim dateString, format As String
    Dim result As Date
    Dim provider As CultureInfo = CultureInfo.InvariantCulture

    ' Parse date-only value with invariant culture.
    dateString = "16.12.2013. 11:30:25.1234"
    format = "dd.MM.yyyy. hh:mm:ss.ffff"
    Try
        result = Date.ParseExact(dateString, format, provider)
        Debug.WriteLine("{0} converts to {1}", dateString, result.ToString())
        dtp_myDate.Value = result
    Catch ex As FormatException
        Debug.WriteLine("{0} is not in the correct format", dateString)
    End Try

Output:

16.12.2013. 11:30:25.1234 converts to 16/12/2013 11:30:25
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top