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"));
string date = recyclingclaim.SubmittalDate.Value.ToString("MM/dd/yyyy");
recyclingclaim.SubmittalDate = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
DateTime newdate = DateTime.ParseExact(strdat esource, "MM/dd/yyyy",null);
ASKER
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!
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 formatA date has no format. It is its own object. Only when visualizing, it gets formatted. Just play with @David's examples.
ASKER
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!
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.
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);
Which produces:
10/28/2021 00:00:00
28/10/2021
10/28/2021 00:00:00
ASKER
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!
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.
ASKER
Because I want to assign recyclingclaim.SubmittalDate = newdate and my recyclingclaim.SubmittalDate is in date format.
Thank you!