Loading, please wait...

A to Z Full Forms and Acronyms

Find the first date of a week from a given date In C#

Dec 28, 2021 CSharp, C# .Net, Date in CSharp, 905 Views
In this article we create a program in c# console application which takes three parameter day of the date, month of the date and year of the date, and then we get the first date of week from that date

In this article, we create a program in c# console application that takes three parameters days of the date, month of the date, and year of the date, and then we get the first date of a week from that date.

Find the first date of a week from a given date In C#

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

  • In the above code first, we take input from users as day, month, and year.
  • Then convert those inputs into proper data and show it to the user.
  • Then get the current culture from System.GlobalizationCultureInfo.  Please check the official documentation for current culture info on the Microsoft website.
  • After this, get the difference between the day of the week of our input date and the first day of the week of current culture.
  • If the difference is less than zero (0) then we add 7 to that difference. Because in next line we minus that difference days from a given date and if pass negative value than it became positive value because we also pass minus (-) before diff in AddDays() methods.
  • AddDays() method adds days in a given date, but here we pass negative value, so it subtracts given days.

Output

Find the first date of a week from a given date In C#

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

Find the first date of a week from a given date In C#

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

Find the first date of a week from a given date In C#
Find the first date of a week from a given date In C#
A to Z Full Forms and Acronyms

Related Article