Horje
Find the first date of a week from a given date In C# Code Example
Find the first date of a week from a given date In C#
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();
        }
    }
}
how to get the date of the first day and last day of the week c#
public static DateTime FirstDayOfWeek(DateTime date)
{
    DayOfWeek fdow = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
    int offset = fdow - date.DayOfWeek;
    DateTime fdowDate = date.AddDays(offset);
    return fdowDate;
}

public static DateTime LastDayOfWeek(DateTime date)
{
    DateTime ldowDate = FirstDayOfWeek(date).AddDays(6);
    return ldowDate;
}




Csharp

Related
C# array to label Code Example C# array to label Code Example
SAVE FLOAT UNITY Code Example SAVE FLOAT UNITY Code Example
asp.net core miniprofiler Code Example asp.net core miniprofiler Code Example
get controller name from ActionExecutingContext .net 4.x Code Example get controller name from ActionExecutingContext .net 4.x Code Example
c# Case insensitive Contains(string) Code Example c# Case insensitive Contains(string) Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
10