Hello, in this blog we will create a program in a C# console application that takes three parameters: day, month, and year of the given date and then we get the first date of the week from that date.

Code

using System;
using System.Globalization;

namespace FirstDateOfWeek
{
    class Program
    {
        static void Main(string[] args)
        {
            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(" Enter the Day : ");
            dt = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Enter the Month : ");
            mn = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Enter 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"));
            var culture = CultureInfo.CurrentCulture; 
            var diff = d.DayOfWeek - culture.DateTimeFormat.FirstDayOfWeek;
            if (diff < 0)
                diff += 7;
            d = d.AddDays(-diff).Date;
            Console.WriteLine(" The first day of the week for the above date is : {0}\n", d.ToString("dd/MM/yyyy"));
            Console.ReadKey();
        }
    }
}

Explanation

Output

In the above image you can see that I entered 15 Dec 2021, and it returns 12 Dec 2021 which is first date of week as you can check in given calendar image.

In my local pc it is by default Sunday as the first day of week, so it returns a date considering Sunday as the first day of week. If I change the first day of the week from the setting, then it returns date as per that.