w3resource logo
C# Sharp Programming Exercises

C# Sharp Exercises: Find the day for a particular date

C# Sharp DateTime: Exercise-57 with Solution

Write a program in C# Sharp to find the day for a particular date.

Sample Solution:-

C# Sharp Code:

using System;

class dttimeex57
{
    static void Main()
    {
        int yr,mn,dt;
        
    Console.Write("\n\n Find the day for a given date :\n");
	Console.Write("------------------------------------\n");        
        
	Console.Write(" Input the Day : ");
    dt = Convert.ToInt32(Console.ReadLine());	
    Console.Write(" Input the Month : ");
    mn = Convert.ToInt32(Console.ReadLine());	
    Console.Write(" Input the Year : ");
    yr = Convert.ToInt32(Console.ReadLine());		
	DateTime d = new DateTime(yr, mn, dt);
	Console.WriteLine(" The formatted Date is : {0}",d.ToString("dd/MM/yyyy"));	
	DateTime pp;
    pp=DayOfWeek(d);
	Console.WriteLine(" The day for the date is : {0}\n ",pp.DayOfWeek);
    }
    public static DateTime DayOfWeek(DateTime dt)
    {
        DateTime ss= new DateTime(dt.Year, dt.Month, dt.Day);
        return ss;
    }    
}

Sample Output:

 Find the day for a given date :                                                                              
------------------------------------                                                                          
 Input the Day : 12                                                                                           
 Input the Month : 06                                                                                         
 Input the Year : 2017                                                                                        
 The formatted Date is : 12/06/2017                                                                           
 The day for the date is : Monday

C# Sharp Code Editor:

using System;

class dttimeex57
{
    static void Main()
    {
        int yr,mn,dt;
        
    Console.Write("\n\n Find the day for a given date :\n");
	Console.Write("------------------------------------\n");        
        
	Console.Write(" Input the Day : ");
    dt = Convert.ToInt32(Console.ReadLine());	
    Console.Write(" Input the Month : ");
    mn = Convert.ToInt32(Console.ReadLine());	
    Console.Write(" Input the Year : ");
    yr = Convert.ToInt32(Console.ReadLine());		
	DateTime d = new DateTime(yr, mn, dt);
	Console.WriteLine(" The formatted Date is : {0}",d.ToString("dd/MM/yyyy"));	
	DateTime pp;
    pp=DayOfWeek(d);
	Console.WriteLine(" The day for the date is : {0}\n ",pp.DayOfWeek);
    }
    public static DateTime DayOfWeek(DateTime dt)
    {
        DateTime ss= new DateTime(dt.Year, dt.Month, dt.Day);
        return ss;
    }    
}

Improve this sample solution and post your code through Disqus