DateTime.Now.AddDays(-1) will get date-time for exactly 24hrs ago, but how do I get the time for yesterday at a specific hour?
51,830
Solution 1
Use:
DateTime.Now.Date.AddDays(-1).AddHours(17);
Solution 2
You can create an instance of TimeSpan
and delete/add in DateTime.Now
.
OR
DateTime dt = DateTime.Now.AddDays(-1);
DateTime newdt = New DateTime(dt.Year, dt.Month, dt.Day, 17, 0, 0);
Author by
user3668266
Updated on August 10, 2022Comments
-
user3668266 over 1 year
I want to somehow specify a time in my
Datetime.Now
so for instance I know thatDateTime.Now.AddDays(-1);
will get the date for yesterday exactly 24hrs ago but I want to get the date for yesterday at a specific time, for instance 5pm.
I can't find anything useful in relation to this on StackOverflow, any suggestions?