2022年5月16日 星期一

[研究][ASP.NET][C#]判斷星期幾

[研究][ASP.NET][C#]判斷星期幾

2022-05-14

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

參考

作法:從特定日期擷取一星期的哪一日 | Microsoft Docs
https://docs.microsoft.com/zh-tw/dotnet/standard/base-types/how-to-extract-the-day-of-the-week-from-a-specific-date

DateTime.DayOfWeek 屬性 (System) | Microsoft Docs
https://docs.microsoft.com/zh-tw/dotnet/api/system.datetime.dayofweek?view=netframework-4.8

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="WebApplication2.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label><br />
        <asp:Label ID="Label99" runat="server" Text="Label"></asp:Label><br />   
    </form>
</body>
</html>


Default.aspx.cs

using System;

namespace WebApplication2
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.DayOfWeek.ToString();    // Monday
            Label2.Text = DateTime.Now.DayOfWeek.ToString("d"); // 1

            DateTime dateValue = new DateTime(2022, 05, 14);
            Label3.Text = dateValue.DayOfWeek.ToString("d");    //6

            DateTime dateValue2 = new DateTime(2022,05,15);
            Label4.Text = dateValue2.DayOfWeek.ToString("d");    //0

            Label5.Text = System.Globalization.CultureInfo.CurrentCulture.
                DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek);    //星期一
            Label6.Text = DateTime.Now.ToString("dddd", 
                new System.Globalization.CultureInfo("zh-tw")); //星期一

            string dd=new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", }
                [Convert.ToInt16(DateTime.Now.DayOfWeek.ToString("D"))];
            Label7.Text = dd;   //星期一

            string dt = DateTime.Today.DayOfWeek.ToString();
            string week = "";
            switch (dt)
            {
                case "Monday":
                    week = "星期一";
                    break;
                case "Tuesday":
                    week = "星期二";
                    break;
                case "Wednesday":
                    week = "星期三";
                    break;
                case "Thursday":
                    week = "星期四";
                    break;
                case "Friday":
                    week = "星期五";
                    break;
                case "Saturday":
                    week = "星期六";
                    break;
                case "Sunday":
                    week = "星期日";
                    break;
            }
            Label99.Text = week;
        }
    }
}

結果

(完)


沒有留言:

張貼留言