I have a code:
int MonthNow = System.DateTime.Now.Month; int YearNow = System.DateTime.Now.Year; int DayNow = System.DateTime.Now.Day;
How can I get yesterday and tomorrow day, month and year in C#?
Of course, I can just write:
DayTommorow = DayNow +1;
but it may happen that tomorrow is other month or year. Are there in C# built-in tools to find out yesterday and today?
Approach Ivar yestedaysDay = DateTime. Now. AddDays(-1); This will give you the exact day with a timestamp of yesterday (24 hours behind the current time).
DateTime tomorrow = DateTime.Today.AddDays(1); DateTime yesterday = DateTime.Today.AddDays(-1);
You can find this info right in the API reference.
var today = DateTime.Today; var tomorrow = today.AddDays(1); var yesterday = today.AddDays(-1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With