How to convert String value to DateTime?

How to convert String value to DateTime?

Like this
CDate(stringvariable.ToString)
Or
If we know the date format exactly in our string then
Like this suppose having date format like dd/MM/yyyy
DateTime.ParseExact(stringvariable.ToString,”dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

Cheers @sivapriya.s

@Palaniyappan

The date I have is in the format - “22/Jan/20”

While doing the above code, I am getting this error

1 Like

from that format to? to which type you need?

@Pradeep_Shiv

The date I have is in the format 22/Jan/20.
I need the same in 22-01-2020 format

Convert.ToDateTime(yourVariableName).ToString(“dd-MM-yyyy”).tostring

DateTime.ParseExact(stringvariable.ToString,“dd/MMM/y”,System.Globalization.CultureInfo.InvariantCulture).toString(“dd-mm-yyyy”)

If date 1 is written as 1, use d/MMM/y. If 1 is written as 01, then dd/MMM/y

Awesome then the expression be like this
DateTime.ParseExact(stringvariable.ToString,”dd/MMM/yy”,System.Globalization.CultureInfo.InvariantCulture)

Cheers @sivapriya.s

1 Like

Use this below code,
DateTime.ParseExact(“22/Jan/20”,“dd/MMM/yy”,System.Globalization.CultureInfo.InvariantCulture).ToString(“dd-MM-yyyy”)

Thank you it worked with Datetime.ParseExact(FirstDate,“dd/MMM/yy”,System.Globalization.CultureInfo.CurrentCulture)

@Palaniyappan

2 Likes

Cheers @sivapriya.s