C# program to get the date of yesterday using TimeSpan

Here, we are going to learn how to get the date of yesterday using TimeSpan in C#?
Submitted by Nidhi, on October 03, 2020

Here we will find yesterday's date using DateTime and TimeSpan class, and then print the yesterday's date on the console screen.

Program:

The source code to get the date of yesterday using TimeSpan is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to get the date of yesterday using TimeSpan.

using System;

class Demo
{
    static void Main()
    {
        DateTime todayDate = DateTime.Now;

        DateTime yesterday;

        yesterday = todayDate - new TimeSpan(1, 0, 0, 0);

        Console.WriteLine("Yesterday: {0}/{1}/{2}", yesterday.Day, yesterday.Month,yesterday.Year);
    }
}

Output:

Yesterday: 12/9/2020
Press any key to continue . . .

Explanation:

Here, we created a class Demo that contains a static method Main(), First of all, here we found the today's date then minus the time-span for 1 day and get the yesterday's date and then print the date in the day, month, and year (DD/MM/YY) format on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.