How to (try) parse a single string in a DateTime in the format "DD / MM / YYYY"? (VB.Net)

How to (try) parse a single string in a DateTime in the format "DD / MM / YYYY"? (VB.Net)

For example: I use the input line "30/12/1999" (December 30, 1999), how (try) to parse it on a DateTime?

+3
source share
1 answer

Try the following:

Dim date As Datetime = DateTime.ParseExact(_
    yourString, "dd/MM/yyyy", CultureInfo.InvariantCulture)

This will throw an exception if it yourStringdoesn't match the format you specified. If you do not want an exception in this case, do the following:

Dim date As Date    
Date.TryParseExact(dateString, "dd/MM/yyyy", CultureInfo.CurrentCulture, _
                      DateTimeStyles.None, date)
+20
source

Source: https://habr.com/ru/post/1744612/


All Articles