check & convert from a string to date vb.net

后端 未结 1 1373
北恋
北恋 2021-01-20 20:25

I am a beginner in VB.NET & I am stuck at a very simple thing, Date formats.

I am working on an application which uploads data from excel sheets into sql server

相关标签:
1条回答
  • 2021-01-20 20:49

    Use DateTime.TryParseExact, which allows you to specify the format - it will attempt to parse it, putting the parsed value into an output parameter on success, and return success/failure:

    Dim dt As DateTime
    If (DateTime.TryParseExact(ROHSDate, "mm/dd/yyyy", \
                               CultureInfo.InvariantCulture, \
                               DateTimeStyles.None, \
                               Out dt))
        UseDateTime(dt)
    End If
    

    Obviously, add Else clauses etc if you need them.

    (I believe in modern VB the line continuations may be unnecessary, but hey... hopefully you get the point.)

    Note that I've explicitly used CultureInfo.InvariantCulture so that if the code is running on a machine which uses different date separators or a different calendar, it won't make any difference.

    0 讨论(0)
提交回复
热议问题