Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to convert datetime in the format dd/MM/yyyy hh:mm:ss tt to yyyy/MM/dd hh:mm:ss tt. I am using asp.net with c# . I tried to convert date time to string and then tried parse exact option but getting string not recognized as datetime exception
Date coming : 12/10/2021 12:00:00 AM (dd/MM/yyyy hh:mm:ss tt)
Need to convert to 2021/10/12 12:00:00 AM format (yyyy/MM/dd hh:mm:ss tt)

What I have tried:

My code is as below:
C#
DateTime _tempDate = DateTime.MinValue;
_tempDate = Convert.ToDateTime(dr[6]);
_tempDate = _tempDate.AddMonths(1);
string date = _TempDate.ToString() DateTime dt = DateTime.ParseExact(date, "yyyy/MM/dd hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);
Posted
Updated 12-Sep-22 14:44pm
v3

DateTime does not have a "format" - it is a datetime.

It is only when you are trying to display a datetime that a "format" becomes visible, and that will mostly depend on where you are in the world, or which Culture you explicitly use.

You can use the .ToString() to format your output e.g.
C#
DateTime _tempDate = DateTime.MinValue;
_tempDate = Convert.ToDateTime(dr[6]);
_tempDate = _tempDate.AddMonths(1);
string date = _tempDate.ToString("yyyy/MM/dd hh:mm:ss tt");
Console.WriteLine(date);
Although personally I would not use Convert.ToDateTime but TryParse or TryParseExact.

There are several ways that you can force the format of the output string - see Custom date and time format strings | Microsoft Docs[^]
 
Share this answer
 
Comments
Maciej Los 12-May-21 12:02pm    
5ed!
You are calling Convert.ToDateTime(dr[6]), and the message is telling you that it does not understand the contents of dr[6]. Change it to use DateTime.ParseExact as you have in the following code.
 
Share this answer
 
Comments
ranio 12-May-21 11:41am    
_tempDate = _tempDate.AddMonths(1);

//Added by Alex on 12.5.2021 to fetch New Bill Date with Holiday Configuration for Loan Postponement Issue Start
string a = _tempDate.ToString("yyyy/MM/dd hh:mm:ss tt");
DateTime dtNew = DateTime.ParseExact(a, "yyyy/MM/dd hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);
on checking string a I am getting date format in yyyy/mm/dd hh:mm:ss tt
But on converting to datetime it is getting changed to dd/mm/yyyy hh:mm:ss tt
How to keep the format as it is
Richard MacCutchan 12-May-21 11:55am    
"But on converting to datetime it is getting changed to dd/mm/yyyy hh:mm:ss tt"
No it is not, since a DateTime object does not have a format, it is a structure that contains an instance in time.

But why are you doing all these conversions? All you need is a DateTime object which you can adjust forwards or backwards as required. Then when you need to display it use the ToString() method to display it in the format you want.
DateTime _tempDate = DateTime.MinValue;
_tempDate = Convert.ToDateTime(dr[6]);
_tempDate = _tempDate.AddMonths(1);
string date = _tempDate.ToString("yyyy/MM/dd hh:mm:ss tt");
Console.WriteLine(date);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900