w3resource

C#: Compute what day was yesterday

C# Sharp DateTime : Exercise-44 with Solution

Write a program in C# Sharp to compute yesterday's date.

Sample Solution:-

C# Sharp Code:

using System;

class dttimeex44
{
    static void Main()
    {
        // Displaying the purpose of the program to the user.
        Console.Write("\n\n Compute what day was Yesterday :\n");
        Console.Write("--------------------------------------\n");

        // Display today's date to the user.
        Console.WriteLine(" Today is : {0}", DateTime.Today.ToString("dd/MM/yyyy"));

        // Calling the GetYesterday method to retrieve yesterday's date.
        DateTime yd = GetYesterday();

        // Display yesterday's date to the user.
        Console.WriteLine(" The Yesterday was : {0} \n", yd.ToString("dd/MM/yyyy"));
    }

    // Method to get yesterday's date by subtracting 1 day from today's date.
    static DateTime GetYesterday()
    {
        return DateTime.Today.AddDays(-1);
    }
}

Sample Output:

Compute what day was  Yesterday :                                                                            
--------------------------------------                                                                        
 Today is : 12/06/2017                                                                                        
 The Yesterday was : 11/06/2017

Flowchart:

Flowchart: C# Sharp Exercises - Compute what day was yesterday

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a program in C# Sharp to check whether the given year, month and day are the current or not.
Next: Write a program in C# Sharp to compute what day will be tomorrow.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.