How can I store only the date portion of the datetime.Today property to a SQL Server table datetime column?

  • A+

I am trying to update a datetime column in a SQL Server 2012 table by using

newItem.DateSaved = DateTime.Today; 

I want to save only the Date part of DateTime, but when I am checking in the table I can see that it saves also the time part: 2018-07-27 00:00:00.000 . How I can make it to store only date part? 2018-07-27 ?


Because my column is datetime, I see that I can't store only the date part. But how I can show to the user only the date part without time? Here is the select part of the Linq-to-SQL query:

select new { Date = smv.DateSaved.Value.Date }).Distinct(); 


A datetime column always has a time part - just, in your case it will all be zeros. A datatime is just a number under the hood; it doesn't have any choice about what it has / doesn't have; similarly, it doesn't have any notion of formatting until your code tries to display it, and then it is the dispalying code that chooses the format.

If you don't want a time part, use date instead of datetime for your column.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: