Link to home
Start Free TrialLog in
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

asked on

Convert date to dd/mm/yyyy format

Hi Experts,

I am trying to convert the date 10/28/2021 to 28/10/2021. These are the different ways I tried.

 string date = recyclingclaim.SubmittalDate.Value.ToString("MM/dd/yyyy");
                                recyclingclaim.SubmittalDate = DateTime.Parse(date, CultureInfo.CreateSpecificCulture("de-DE"));

Open in new window


string date = recyclingclaim.SubmittalDate.Value.ToString("MM/dd/yyyy");
                            recyclingclaim.SubmittalDate = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);

Open in new window

But I am getting "string is not recognized as a valid datetime", it works good for 11/1/2021 and other dates.


Please suggest. Thanks in advance!

ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

ASKER

Thank you so much for trying to help me. Is it possible to convert newdate  to date time instead of string?
Because I want to assign recyclingclaim.SubmittalDate = newdate  and my recyclingclaim.SubmittalDate is in date format.

Thank you!
 
DateTime newdate = DateTime.ParseExact(strdatesource, "MM/dd/yyyy",null);
Actually I want the date in dd/mm/yyyy format. Something like this.
DateTime newdate = DateTime.ParseExact(strdatesource, "dd/MM/yyyy",null);
But it is giving error. Any workaround to this?

Thank you for ur time!

mydate is a datetime object
string newdate = mydate.ToString("dd'/'MM'/'yyyy"); //fixup to change separator
Actually I want the date in dd/mm/yyyy format
A date has no format. It is its own object. Only when visualizing, it gets formatted. Just play with @David's examples.
I can get the date to string variable. But after I get the date in dd/mm/yyyy which is a string at this point, is it possible to convert  to datetime format again? In this case can I make newdate  as datetime (dd/mm/yyyy)?

Thank you!
a datetime is as Stefan stated an object.. which is the offset from the epoch time to the time saved
the datetime object can be displayed as a string using formatting commands. a string is not a datetime object.
I can get the date to string variable. But after I get the date in dd/mm/yyyy which is a string at this point, is it possible to convert  to datetime format again?
Where is the breakdown exactly?  You converted it to a string with a specific format, so just convert it back to a DateTime using the same format that you specified before:
DateTime dt1 = new DateTime(2021, 10, 28);

String format = "dd/MM/yyyy";
String strDt1 = dt1.ToString(format);

DateTime dt2 = DateTime.ParseExact(strDt1, format, null);

Console.WriteLine(dt1);
Console.WriteLine(strDt1);
Console.WriteLine(dt2);

Open in new window


Which produces:
10/28/2021 00:00:00
28/10/2021
10/28/2021 00:00:00

Open in new window

Hi Mike,
I want dt2 as 28/10/2021. But I understood that we can not do that because date time is an object as David said. Thank you all for your help!
You need to distinguish between a date aka a date object and a date literal aka a date string. When we talk about date, then in .NET we always mean date object, never date literal. Thus always use a clear phrasing of what you like to do.