Click here to Skip to main content
15,747,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, so I have this data model class in my .Net Web API:

public class ExcelReport
      public string ProjectName { get; set; }
      public DateTime CreatedOn { get; set; }

Right now it saves the date to such format in the database: 2020-08-01T00:00:00.

What I want to achieve is getting rid of everything after the date, without changing the data type to string (as this column in the database uses the DATE type, not VARCHAR). Is it possible?

What I have tried:

I have played around with .ToShortDateString method and the like, but don't think it's suited to this particular problem.
Updated 23-Jan-22 5:35am
Richard MacCutchan 23-Jan-22 14:12pm    
"Right now it saves the date to such format in the database: 2020-08-01T00:00:00."
No it does not. It stores it as a DateTime object, which has no format. It only appears in any particular format when you try to display it.

1 solution

That's stored in an appropriate database type. What you see in the database has nothing at all to do with your code.

If you want to store only the date, you have to change the datatype in the database to 'date'. Read the documentation on SQL Server for date/time datatypes here[^].
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