3

how do i convert the string

30.10.2009 in date? (dd.mm.yyyy)

thanks :>

2 Answers 2

4

You could use the TryParseExact function:

Dim DateStr = "30.10.2009"
Dim Dt As DateTime
If DateTime.TryParseExact(DateStr, "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, Dt) Then
    ' The date was successfully parsed => use the Dt variable
End If
Sign up to request clarification or add additional context in comments.

2 Comments

it says, that "CultureInfo" and "DateTimeStyles" is not declared- do i have to include something?
Yes, the System.Globalization namespace.
2

You can use DateTime.ParseExact:

Dim culture as CultureInfo = new CultureInfo("en-US")
Dim date as DateTime = DateTime.ParseExact("30.10.2009", "dd.MM.yyyy", culture)

See custom datetime format strings on MSDN.

If you are not sure that the format is exactly as mentioned, you can use TryParseExact to avoid an exception being thrown.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.