Reputation: 9496
How would i save the date time, with no time or preferably with a time stamp of 12:00, no matter what the time is at that moment?
I dont want to use .ToString("dd/MM/yyyy");
, because it will open up a whole lot of new possible errors.
Upvotes: 7
Views: 21914
Reputation: 4115
As other posters have said
DateTime.Now.Date
is the way to go. However it is also worth thinking about whether you want to strip out timezone information to prevent problems occurring across different TZ on client\server machines.
You can use
var dateTime = new DateTime(DateTime.Now.Date.Ticks, DateTimeKind.Unspecified);
Upvotes: 2
Reputation: 7376
Take a look at this http://msdn.microsoft.com/en-us/library/system.datetime.today.aspx
If you want the current date, there is a slightly more readable:
DateTime.Today
but for a specific DateTime instance, then:
myDateTime.Date
Upvotes: 1
Reputation: 9113
This will do the trick with a preference to ts.Date
var ts = DateTime.Now;
var dateAtMidnight = ts.Date;
var dateAtNoon = ts.Date.AddHours(12);
Upvotes: 1
Reputation: 47058
If you have any date
DateTime anyDate = DateTime.Now;
DateTime dateAtNoon = anyDate.Date.AddHours(12);
or if you want today you can use the shortcut
DateTime dateAtNoon = DateTime.Today.AddHours(12);
Upvotes: 1
Reputation: 422202
DateTime
struct has a Date
property that should serve your needs:
DateTime dateOnly = dateTime.Date;
Of course, it'll still inevitably contain a time part but you should be able to ignore it.
Upvotes: 15