How to GET yesterday’s Date using C#

Today in this article we shall see , how to get yesterday’s date using C# code.

Approach I

 var yestedaysDay = DateTime.Now.AddDays(-1);

This will give you the exact day with a timestamp of yesterday (24 hours behind the current time).

6/1/2020 5:34:48 PM

If you need any other format of dates, then format the date in a format of your choice using below.

Example 1

var yestedaysDay = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
"2021-06-01"

Example 2

var yestedaysDay = DateTime.Now.AddDays(-1).ToString("MM-dd-yyyy");

“06-01-2021”

Approach II

var yestedaysDay = DateTime.Today.AddDays(-1);

This will give you the exact day with a timestamp 12:00:00 of yesterday.

6/1/2020 12:00:00 AM

References

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *