Click here to Skip to main content
15,358,440 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Currently I have
DateTime? InstallDate ;
which is in format '25-01-2022 13:27:36'

I am trying to convert into format
2014-12-23 00:00:00.000


What I have tried:

string date = MeterInstallDate.ToString();
date.ToString("yyyy-MM-dd hh:mm:ss");
//Now I have convert it back to datetime i.e in the variable InstallDate 
Posted
Updated 30-Jan-22 19:16pm
Comments
Himansh jain 27-Jan-22 7:58am
   
Here the variable is InstallDate not MeterInstallDate
Richard MacCutchan 27-Jan-22 8:03am
   
You are converting MeterInstallDate to a string, and than calling ToString on that string, which all makes no sense.

You need something like:
InstallDate = MeterInstallDate;
string DateOfInstallation = InstallDate.ToString("yyyy-MM-dd hh:mm:ss");
Himansh jain 28-Jan-22 23:30pm
   
DateTime? InstallDate = '25-01-2022 13:27:36';


I have to convert it into format(yyyy/mm/dd hh:mm:ss) as my database where I have to insert the InstallDate takes only the (yyyy/mm/dd hh:mm:ss) format.
Desired Output
InstallDate = '2022-01-25 13:27:36'

Please provide the code if you can figure this! Thankyou for your support.
Richard MacCutchan 29-Jan-22 3:32am
   
I already gave you the code above. You seem to think that a DateTime type is a string variable. I suggest you go to DateTime Struct (System) | Microsoft Docs[^] to understand it properly.

DateTime does not have a format.

THe format only applies when you convert the DateTime to a string for display. There is a default format that is used if you don't specify the format you want.

If you have the original DateTime object, why would need to convert it to a string just to convert it back to a DateTime? Just set one variable equal to the other.
   
Comments
Himansh jain 28-Jan-22 23:28pm
   
DateTime? InstallDate = '25-01-2022 13:27:36';


I have to convert it into format(yyyy/mm/dd hh:mm:ss) as my database where I have to insert the InstallDate takes only the (yyyy/mm/dd hh:mm:ss) format.
InstallDate = '2022-01-25 13:27:36'

Please provide the code if you can figure this! Thankyou for your support.
Dave Kreskowiak 28-Jan-22 23:34pm
   
No, you don't. Database datetimes do not have a format. If you're storing a datetime as string in the database, you're causing yourself a huge problem.

I have no idea what database engine you're using, so about the only thing anyone can tell you is to use parameterized queries. The parameter will take care of "formatting" the datetime for you.
Further to Solution 1 it looks like you want to remove the time element of your DateTime. Normally you can just create a new date using the DateTime constructor that takes the elements of the original date that you want to retain. But because you have a nullable DateTime you have to insert an interim step e.g.
C#
DateTime? InstallDate = new DateTime(2022,01,25,13,27,36);
DateTime notNullDate = InstallDate.GetValueOrDefault(DateTime.Now);
DateTime ConvDate = new DateTime(notNullDate.Year, notNullDate.Month, notNullDate.Day, 0,0,0);
   
Comments
Himansh jain 28-Jan-22 23:29pm
   
DateTime? InstallDate = '25-01-2022 13:27:36';


I have to convert it into format(yyyy/mm/dd hh:mm:ss) as my database where I have to insert the InstallDate takes only the (yyyy/mm/dd hh:mm:ss) format.
Desired Output
InstallDate = '2022-01-25 13:27:36'

Please provide the code if you can figure this! Thankyou for your support.
CHill60 29-Jan-22 0:59am
   
Hm. How about
DateTime? InstallDate='2022-01-25 13:27:36';
Your database does NOT take dates in a "format" ...it takes valid dates. You should not be "converting" anything. How about telling us the real problem?
Convert.ToDateTime(InstallDate).ToString("yyyy/MM/dd HH:mm:ss")
   
Comments
CHill60 31-Jan-22 3:31am
   
As I said in my comment above - you do not need to convert anything to a string if you are storing a date. InstallDate is a DateTime? which you are converting to a DateTime just to convert to a string. This is absolutely the wrong way to deal with dates

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