Asp.net with c# example - datetime day of week

c# example - date time day of week:


DateTime DayOfWeek property allows us to get the day number of week from a datetime object. this DayOfWeek property value type is System.DayOfWeek. the System.DayOfWeek value is an enumerated constant that indicates the day of the week of the provided datetime object.




DayOfWeek enumeration ranges from Sunday to Saturday. so if the datetime object is Sunday then it returns 0 (zero), Monday 1, Tuesday 2 and so on when we cast the value to an integer. The return integer number starts from zero. by this way we can get day number from a datetime object. Finally we cast the return value to an integer value. We also can get the day name by using this DayOfWeek property.

The following .net c# example code demonstrate us how can we get the day number as an integer value from a datetime object in an asp.net application.

<!DOCTYPE html>       
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e) 
    { 
        //initialize a datetime variable with today 
        DateTime today = DateTime.Today; 
 
        Label1.Text = "today : " + today.ToLongDateString(); 
 
        Label1.Text += "<br ><br />day of week as string: "; 
        Label1.Text += today.DayOfWeek.ToString(); 
 
        //Sunday is first day of week 
        //get day number of week 
        int dayNumberOfWeek = (int)today.DayOfWeek; 
 
        Label1.Text += "<br ><br />day of week as number (integer): "; 
        Label1.Text += dayNumberOfWeek; 
    } 
</script>       
       
<html xmlns="http://www.w3.org/1999/xhtml">       
<head id="Head1" runat="server">       
    <title>c# example - datetime day of week int</title>       
</head>       
<body>       
    <form id="form1" runat="server">       
    <div>       
        <h2 style="color:MidnightBlue; font-style:italic;">       
            c# example - datetime day of week int 
        </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 day of week as number"       
            OnClick="Button1_Click"     
            Height="40"       
            Font-Bold="true"       
            />       
    </div>       
    </form>       
</body>       

</html>  

Comments

Post a Comment