Converting a String Date to Date Format Error: 'String is not a Valid DateTime'

100 Views Asked by At

I want to convert string format date value to date format ("dd/MM/yyyy").

String EntryDate = "24052023"; 

DateTime ResultDate = Convert.ToDateTime(EntryDate);

DateTime ResultDate = Convert.ToDateTime(EntryDate).ToStringShortDate("dd/mm/yyyy");

But it's not working. Show error like string not valid datetime.

1

There are 1 best solutions below

0
arbabu On

Here is the solution:

string exp_date="24052023"; //string date

string ConvertExpDate = DateTime.ParseExact(exp_date,"ddMMyyyy", CultureInfo.InvariantCulture).ToString("dd'/'MM'/'yyyy");

//Result 
ConvertExpDate = 24/05/2023;

This code works.