String to Date Converstion

Hi everyone,

i have an issue in converting string to Date format.
string = ‘10-8-2021’
i want to convert this string to date like 10-08-2021/10-8-2021.
if the string is like 10-08-2021 then i can able to convert to date.
but i am getting the string as ‘10-8-2021’. not able to convert.
and i also dot need time span.
Please help. TIA

Hi @SudhaT

Try this expression

CDate(“10-8-2021’”).Tostring(“dd-MM-yyyy”)

Regards
Gokul

Try this approach as well

@SudhaT

DateTime.ParseExact(“10-8-2021”,“dd-M-yyyy”,System.Globalization.CulturalInfo.InvariantCulture)

Hi SudhaT,

Please check this link . Hope it helps you

datetimes don’t have formats. “10-08-2021” is not a date, it’s a string. If you want to change how it’s displayed, convert it to date and then back to string with a different format. For Example…

Datetime.Parse(“10-08-2021”).ToString(“MM/dd/yyyy”)

…will output 10/08/2021

Hi @SudhaT ,

Is your String having the Single Quotes and . mark as an extra ?

If the extracted String always contains these characters, we could perform a Replace of these characters first and then Perform Conversion to Date in the below way :

DateTime.ParseExact("'10-8-2021'".Replace("'","").Replace(".",""),"dd-M-yyyy",System.Globalization.CultureInfo.InvariantCulture)

Hi @SudhaT,

You can try Below -

DateTime.ParseExact(“‘10-8-2021’”.Replace(“'”,“”),“dd-M-yyyy”,System.Globalization.CultureInfo.InvariantCulture).ToString(“dd-MM-yyyy”)

Thanks