c# - How to get the integer value of day of week

DateTime day of week int
DateTime DayOfWeek property allow 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 start 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.

<%@ Page Language="C#" AutoEventWireup="true"%>
<!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;
<html xmlns="http://www.w3.org/1999/xhtml">      
<head id="Head1" runat="server">      
    <title>c# example - datetime day of week int</title>      
    <form id="form1" runat="server">      
        <h2 style="color:MidnightBlue; font-style:italic;">      
            c# example - datetime day of week int
        <hr width="550" align="left" color="Gainsboro" />      
            Font-Names="Comic Sans MS"
        <br /><br />    
            Text="get day of week as number"      
More c# examples