w3resource logo
C# Sharp Programming Exercises

C# Sharp Exercises: Compute what day was yesterday

C# Sharp DateTime : Exercise-44 with Solution

Write a program in C# Sharp to compute what day was yesterday.

Sample Solution:-

C# Sharp Code:

using System;

class dttimeex44
{
    static void Main()
    {
	Console.Write("\n\n Compute what day was  Yesterday :\n");
	Console.Write("--------------------------------------\n");
	Console.WriteLine(" Today is : {0}", DateTime.Today.ToString("dd/MM/yyyy"));
	DateTime yd = GetYesterday();
	Console.WriteLine(" The Yesterday was : {0} \n", yd.ToString("dd/MM/yyyy"));
    }
    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

C# Sharp Code Editor:

using System;

class dttimeex44
{
    static void Main()
    {
	Console.Write("\n\n Compute what day was  Yesterday :\n");
	Console.Write("--------------------------------------\n");
	Console.WriteLine(" Today is : {0}", DateTime.Today.ToString("dd/MM/yyyy"));
	DateTime yd = GetYesterday();
	Console.WriteLine(" The Yesterday was : {0} \n", yd.ToString("dd/MM/yyyy"));
    }
    static DateTime GetYesterday()
    {
	return DateTime.Today.AddDays(-1);
    }
}

Improve this sample solution and post your code through Disqus