Home > Software engineering >  How to remove hours from datetime
How to remove hours from datetime

Time:08-27

I'm trying to map through old db and take the dates which are in 2014-08-01 format and add them to the new DB. However, in the new DB along with the date default time is set,example 2014-10-06 00:00:00.0000000

newContract.ContractDate = DateTime.Parse(dataReader[2].ToString());
newContract.EndDate = DateTime.Parse(dataReader[3].ToString());

I tried like this:

newContract.ContractDate = DateTime.Parse(dataReader[2].ToString("yyyy-mm-DD"));

but I got error message:

Error   CS1501  No overload for method 'ToString' takes 1 arguments ODWE    

Any help is appreciated, thank you in advance!

CodePudding user response:

The issue is coming from the fact that you are trying to format an object which is not a datetime.

dataReader[2] is not a DateTime, it's an object that you are parsing as a string using toString() but the toString() from object does not allow you to format.

And the default time is normal, that's the way datetime are stored because as the name suggest, it's date and time not only date. You cannot save only date in DateTime you can only have the time set to 00:00:00.0000000, but in your code you can format your newContract.ContractDate.ToString("yyyy MMMM dd") to display the date the way you want.

Hope it helps

CodePudding user response:

Try this if you want only date.

newContract.ContractDate = Convert.ToDateTime(dataReader[2]).ToString("MM/dd/yyyy");
  • Related