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.
.ToString("dd/MM/yyyy");
DateTimestruct has a property Datethat should suit your needs:
DateTime
Date
DateTime dateOnly = dateTime.Date;
Of course, it will inevitably still contain a temporary part, but you must be able to ignore it.
As other posters said
DateTime.Now.Date
- . , , , TZ \ .
var dateTime = new DateTime(DateTime.Now.Date.Ticks, DateTimeKind.Unspecified);
Using
or for myDate:
myDate.Date
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);
This will do the ts.Date preference trick.
var ts = DateTime.Now; var dateAtMidnight = ts.Date; var dateAtNoon = ts.Date.AddHours(12);
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