w3resource logo
C# Sharp Programming Exercises

C# Sharp Exercises: Find the last day of a week against a given date

C# Sharp DateTime: Exercise-53 with Solution

Write a program in C# Sharp to find the last day of a week against a given date.

Sample Solution:-

C# Sharp Code:

using System;

class dttimeex53
{
    static void Main()
    {
	int yr,mn,dt;
    Console.Write("\n\n Find the first day of a week against 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"));
	Console.WriteLine(" The first day of the week for the above date is : {0}\n", FirstDayOfWeek(d).ToString("dd/MM/yyyy"));
    }
   public static DateTime FirstDayOfWeek(DateTime dt)
    {
        var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
        var diff = dt.DayOfWeek - culture.DateTimeFormat.FirstDayOfWeek;
        if(diff < 0) 
            diff += 7;
        return dt.AddDays(-diff).Date;
    }
}

Sample Output:

 Find the last day of a week against a given date :                                                           
-------------------------------------------------------                                                       
 Input the Day : 12                                                                                           
 Input the Month : 06                                                                                          
 Input the Year : 2017                                                                                        
 The formatted Date is : 12/06/2017                                                                           
 The last day of the week for the above date is : 17/06/2017 

C# Sharp Code Editor:

using System;

class dttimeex53
{
    static void Main()
    {
	int yr,mn,dt;
    Console.Write("\n\n Find the last day of a week against 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"));
	Console.WriteLine(" The last day of the week for the above date is : {0}\n", LastDayOfWeek(d).ToString("dd/MM/yyyy"));
    }
   public static DateTime LastDayOfWeek(DateTime dt)
    {
        var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
        var diff = dt.DayOfWeek - culture.DateTimeFormat.FirstDayOfWeek;
        if(diff < 0) 
            diff += 7;
        return dt.AddDays(-diff+6).Date;
    }
}

Improve this sample solution and post your code through Disqus