Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

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;
}
Comment

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("

 Find the first day of a week against a given date :
");
            Console.Write("--------------------------------------------------------
");

            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}
", d.ToString("dd/MM/yyyy"));
            Console.ReadKey();
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# inline if 
Csharp :: how to copy last element in list c# 
Csharp :: c# mailmessage set sender name 
Csharp :: how to set up blender with unity units 
Csharp :: c# 
Csharp :: triangle minimum path sum c# 
Csharp :: c# write variable in string 
Csharp :: change sprite of a sprite unity 
Csharp :: how to make a singleton in unity 
Csharp :: c# map 
Csharp :: mvc 5 dropdownlist 
Csharp :: accessing form controls from another class c# 
Csharp :: increase timeout in .net core web app 
Csharp :: 2d list in c# 
Csharp :: instantiate list with values c# 
Csharp :: list all files in directory and subdirectories c# 
Csharp :: c# ignore enter key 
Csharp :: convert object to array in c# 
Csharp :: c# byte array to file 
Csharp :: c# find all indexes 
Csharp :: WPF Confirmation MessageBox 
Csharp :: move files from one folder to another using c# 
Csharp :: get char lowercase in c# 
Csharp :: c# contains 
Csharp :: unity exception 
Csharp :: C# array of repeated value 
Csharp :: color unity 
Csharp :: project mongodb c# 
Csharp :: get index c# 
Csharp :: string to camel case c# 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =