How to save date time without time?

How can I save a date without any time, or preferably with a timestamp of 12:00, no matter what time it is at that moment?

I do not want to use .ToString("dd/MM/yyyy");, because it will open up many new possible errors.

+5
source share
6 answers

DateTimestruct has a property Datethat should suit your needs:

DateTime dateOnly = dateTime.Date;

Of course, it will inevitably still contain a temporary part, but you must be able to ignore it.

+14
source

As other posters said

DateTime.Now.Date 

- . , , , TZ \ .

var dateTime = new DateTime(DateTime.Now.Date.Ticks, DateTimeKind.Unspecified);
+2

Using

 DateTime.Now.Date

or for myDate:

 myDate.Date
+1
source

If you have a date

DateTime anyDate = DateTime.Now;
DateTime dateAtNoon = anyDate.Date.AddHours(12);

or if you want today you can use a shortcut

DateTime dateAtNoon = DateTime.Today.AddHours(12);
+1
source

This will do the ts.Date preference trick.

var ts = DateTime.Now;
var dateAtMidnight = ts.Date;
var dateAtNoon = ts.Date.AddHours(12);
+1
source

Take a look at this http://msdn.microsoft.com/en-us/library/system.datetime.today.aspx

If you need a current date, there is a slightly more readable one:

DateTime.Today

but for a specific instance of DateTime:

myDateTime.Date
+1
source

All Articles