Skip to content

Get Start of Each Week for Current Month

< %@ Page Language="C#" AutoEventWireup="true"  CodeFile="default.aspx.cs" Inherits="_Default" %>                                                    <html>
<p><asp:label runat="server" id="GreetingLabel"></asp:label></p>
<p><asp:label runat="server" id="WeekLabel"></asp:label></p>
<p><asp:label runat="server" id="SeasonLabel"></asp:label></p>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Globalization;
using System.Web;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Set Current DateTime
        DateTime Today = DateTime.Today;
        
        //Create Display Greeting
        int CurrentHour = int.Parse(DateTime.Now.ToString("HH"));
        GreetingLabel.Text = GetGreeting(CurrentHour) + "Happy " + Today.DayOfWeek.ToString() + "<br />";
        
        //Create Season Hours
        int CurrentMonth = int.Parse(Today.Month.ToString());
        SeasonLabel.Text = "<a href=\"" + GetSeason(CurrentMonth) + Today.ToString("yyyy") + ".xlsx\">" + GetSeason(CurrentMonth) + " " + Today.ToString("yyyy") + " Hours" + "</a>";    
        
        //Create Weeks for the Month
        //Calculate Number of Days in the Month and Print the Weeks for the Month
        //DateTime FirstDayOfCurrentMonth = new DateTime(Today.Year, Today.Month, 1);
        DateTime FirstDayOfCurrentMonth = DateTime.Parse("2016/01/01 18:37:58");
        DateTime FirstDayOfNextMonth = FirstDayOfCurrentMonth.AddMonths(1);
        DateTime FirstDayOfCurrentWeek = GetFirstDayOfWeek(FirstDayOfCurrentMonth, DayOfWeek.Sunday);
        while (FirstDayOfCurrentWeek < FirstDayOfNextMonth)
        {
            WeekLabel.Text += "<p>Week of " + FirstDayOfCurrentWeek.ToString("MMMM") + " " + FirstDayOfCurrentWeek.Day + GetDateSuffix(FirstDayOfCurrentWeek.Day) + "";
            
            FirstDayOfCurrentWeek = FirstDayOfCurrentWeek.AddDays(7);
        }
    }
    
    public static DateTime GetFirstDayOfWeek(DateTime dayInWeek, DayOfWeek firstDay) 
    {
        int difference = ((int)dayInWeek.DayOfWeek) - ((int)firstDay);
        difference = (7 + difference) % 7;
        return dayInWeek.AddDays(-difference).Date;
    }
    public static DateTime GetFirstDayOfWeek(DateTime dayInWeek) 
    {
        return GetFirstDayOfWeek(dayInWeek, CultureInfo.CurrentCulture);
    }
    
    public static DateTime GetFirstDayOfWeek(DateTime dayInWeek, CultureInfo cultureInfo) 
    {
        return GetFirstDayOfWeek(dayInWeek, cultureInfo.DateTimeFormat.FirstDayOfWeek);
    }
    public    static string GetGreeting(int Hour)
    { 
        string Greeting = "";    
        
        if (Hour < 12)
        {
            Greeting = "Good Morning! ";
        }
        else if (Hour > 12 && Hour < 17) 
        {
            Greeting = "Good Afternoon! ";
        }
        else
        {
            Greeting = "Good Evening! ";
        }
            
        return Greeting;
     }
    public    static string GetDateSuffix(int Day)
    { 
        string DaySuffix = "";
        
        switch (Day)
        {
            case 1:
            case 21:
            case 31:
                DaySuffix = "st";
                break;
            case 2:
            case 22:
                DaySuffix = "nd";
                break;
            case 3:
            case 23:
                DaySuffix = "rd";
                break;
            default:
                DaySuffix = "th";
                break;
        }
            
        return DaySuffix;
     }
    public    static string GetSeason(int Month)
    { 
        string Season = "";
        
        if (Month >= 1 || Month < 5)
        {
            Season = "Spring";
        }
        else if (Month >= 5 || Month < 9)
        {
            Season = "Summer";
        }
        else 
        {
            Season = "Fall";
        }
            
        return Season;
     }     
}