C#: Find the day for a particular date
C# Sharp DateTime: Exercise-57 with Solution
Write a program in C# Sharp to find the day on a particular date.
Sample Solution:-
C# Sharp Code:
using System; // Importing the System namespace
class dttimeex57 // Declaring a class named dttimeex57
{
static void Main() // Declaring the Main method
{
int yr, mn, dt; // Declaring variables for year, month, and day
Console.Write("\n\n Find the day for a given date :\n"); // Displaying a message in the console
Console.Write("------------------------------------\n"); // Displaying a separator line
// Input prompts for day, month, and year
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); // Creating a DateTime object with the provided year, month, and day
Console.WriteLine(" The formatted Date is : {0}", d.ToString("dd/MM/yyyy")); // Displaying the formatted input date
DateTime pp; // Declaring a DateTime variable
pp = DayOfWeek(d); // Calling the DayOfWeek method to get the day for the given date
Console.WriteLine(" The day for the date is : {0}\n ", pp.DayOfWeek); // Displaying the day for the given date
}
// Method to get the day of the week for a specific date
public static DateTime DayOfWeek(DateTime dt)
{
DateTime ss = new DateTime(dt.Year, dt.Month, dt.Day); // Creating a new DateTime object representing the same date as the input date
return ss; // Returning the date
}
}
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
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a program in C# Sharp to find the First day of next month against a given date.
Next: C# Sharp File Handling Exercises.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/csharp-exercises/datetime/csharp-datetime-exercise-57.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics