c# - How to get the first day of week

DateTime first day of week
datetime-first-day-of-week.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>
    
<!DOCTYPE html>      
<script runat="server">
    public static DateTime getFirstDayOfWeek(DateTime dateToCheck, string firstDay)
    {
        DateTime dt=new DateTime();
        for (int i = 0; i < 7; i++)
        {
            if (dateToCheck.AddDays(-i).DayOfWeek.ToString() == firstDay)
            {
                dt= dateToCheck.AddDays(-i);
            }
        }
        return dt;
    }
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        //initialize a datetime variable with today
        DateTime today = DateTime.Today;

        Label1.Text = "today : " + today.ToLongDateString();

        DateTime firstDayOfWeek = getFirstDayOfWeek(today, "Monday");

        Label1.Text += "<br ><br />start day of week : ";
        Label1.Text += firstDayOfWeek.ToLongDateString();
    }
</script>      
      
<html xmlns="http://www.w3.org/1999/xhtml">      
<head id="Head1" runat="server">      
    <title>c# example - datetime first day of week</title>      
</head>      
<body>      
    <form id="form1" runat="server">      
    <div>      
        <h2 style="color:MidnightBlue; font-style:italic;">      
            c# example - datetime first day of week
        </h2>      
        <hr width="550" align="left" color="Gainsboro" />      
        <asp:Label       
            ID="Label1"       
            runat="server"      
            Font-Size="Large"    
            Font-Names="Comic Sans MS"
            >      
        </asp:Label>      
        <br /><br />    
        <asp:Button       
            ID="Button1"       
            runat="server"       
            Text="get first day of week"      
            OnClick="Button1_Click"    
            Height="40"      
            Font-Bold="true"      
            />      
    </div>      
    </form>      
</body>      
</html>
More c# examples