Dates Fundamentals

Introduction

A date is a measure of non-spatial units that have elapsed in a set period. A group of seven consecutive days is called a week. The days of a week are named in US English as Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday. These are referred to as long names. The corresponding short names in US English are Mon, Tue, Wed, Thu, Fri, Sat, and Sun. In US English, Sunday is usually considered the first day of the week.

The months are counted from 1 and up. Each month holds a long name. In US English, they are January, February, March, April, May, June, July, August, September, October, November, and December. Their short names in US English are Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, and Dec.

A group of four months is called a quarter. A group of twelve months can be called a year. In the .NET library, the years are counted from 0001 to 9999. In a regular year, the months are counted from 1 to 12.

The technique or expression used to identify a particular day during a year is called a date. The days are counted from 1 to 365 or 366 (depending on a factor called a leap year). A date can be the combination of a day, its month, and the year.

Practical LearningPractical Learning: Introducing Date Values

  1. Start Microsoft Visual Studio and create a new ASP.NET Core Web App (that uses .NET 6.0 (Long-Term Support)) named CountriesStatistics2
  2. In the Solution Explorer, expand wwwroot
  3. In the Solution Explorer, under wwwroot, right-click css -> Add -> New Item...
  4. In the left list of the Add New Item dialog box, under Visual C#, expand ASP.NET Core and expand Web. Click Content.
    In the middle list of the Add New Item dialog box, click Style Sheet
  5. Change the file Name to CountriesStatistics
  6. Click Add
  7. Change the document as follows:
    body {
    }
    .bold                             { font-weight:      bold;               }
    .text-right                       { text-align:       right;              }
    .top-bar                          { border-bottom:    6px solid blue;
                                        background-color: #000000 !important; }
    .navbar-light .navbar-brand       { color:            white;              }
    .navbar-light .navbar-brand:hover { color:            yellow;             }
    .navbar-light .navbar-brand:focus { color:            khaki;              }
    .navbar-light .navbar-brand       { font-family:      Georgia, Garamond, 'Times New Roman', serif; }
    .nav-link                         { font-family:      Georgia, Garamond, 'Times New Roman', serif; }
    .common-font                      { font-family:      Georgia, Garamond, 'Times New Roman', serif; }
  8. In the Solution Explorer, expand Pages and expand Shared
  9. In the Solution Explorer, under Pages and under Shared, and double-click _Layout.cshtml
  10. Change the document as follows:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>@ViewData["Title"] - Countries Statistics</title>
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
        <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
        <link rel="stylesheet" href="~/css/CountriesStatistics.css" asp-append-version="true" />
    </head>
    <body>
        <header>
            <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3 top-bar">
                <div class="container">
                    <a class="navbar-brand" asp-area="" asp-page="/Index">Countries Statistics</a>
                    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                            aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
                    <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                        <ul class="navbar-nav flex-grow-1">
                            <li class="nav-item">
                                <a class="nav-link text-white" asp-area="" asp-page="/Index">Home</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-white" asp-area="" asp-page="/Privacy">Privacy</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </nav>
        </header>
        <div class="container">
            <main role="main" class="pb-3">
                @RenderBody()
            </main>
        </div>
    
        <footer class="border-top footer text-muted">
            <div class="container">
                <p class="text-center common-font">&copy; 2022 - Countries Statistics - <a asp-area="" asp-page="/Privacy">Privacy</a></p>
            </div>
        </footer>
    
        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
        <script src="~/js/site.js" asp-append-version="true"></script>
    
        @await RenderSectionAsync("Scripts", required: false)
    </body>
    </html>
  11. In the Solution Explorer, under Pages, double-click Index.cshtml
  12. To create and use arrays, change the document as follows:
    @page
    @model IndexModel
    @{
        string[] states = new string[31];
    
        states[0]  = "Guanajuato";      states[1]  = "Tamaulipas";          states[2]  = "Michoacán";  states[3]  = "Coahuila";
        states[4]  = "Chihuahua";       states[5]  = "Baja California Sur"; states[6]  = "Nayarit";    states[7]  = "Puebla";
        states[8]  = "Oaxaca";          states[9]  = "Morelos";             states[10] = "Sonora";     states[11] = "Aguascalientes";
        states[12] = "Baja California"; states[13] = "Tabasco";             states[14] = "Jalisco";    states[15] = "México";
        states[16] = "Guerrero";        states[17] = "Colima";              states[18] = "Zacatecas";  states[19] = "Sinaloa";
        states[20] = "Campeche";        states[21] = "Quintana Roo";        states[22] = "Nuevo León"; states[23] = "Hidalgo";
        states[24] = "Tlaxcala";        states[25] = "Yucatán";             states[26] = "Querétaro";  states[27] = "Veracruz";
        states[28] = "San Luis Potosí"; states[29] = "Durango";             states[30] = "Chiapas";
    
        string[] capitals = new string[] { "Guanajuato",                "Ciudad Victoria",           "Morelia",               "Saltillo",         "Chihuahua",
                                           "La Paz",                    "Tepic",                     "Puebla de Zaragoza",    "Oaxaca de Juárez", "Cuernavaca",
                                           "Hermosillo",                "Aguascalientes",            "Mexicali",              "Villahermosa",     "Guadalajara",
                                           "Toluca de Lerdo",           "Chilpancingo de los Bravo", "Colima",                "Zacatecas",        "Culiacán",
                                           "San Francisco de Campeche", "Chetumal",                  "Monterrey",             "Pachuca",
                                           "Tlaxcala",                  "Mérida",                    "Santiago de Querétaro", "Xalapa",
                                           "San Luis Potosí",           "Victoria de Durango",       "Tuxtla Gutiérrez"};
        int[] areasSqrKms                   = new int[] {  30608, 80175, 58643, 151563,  247455,  73922, 27815, 34290, 93793,   4893,
                                                          179503,  5618, 71446,  24738,   78599,  22357, 63621,  5625, 75539,  57377,
                                                           57924, 42361, 64220,  20846,    3991,  39612, 11684, 71820, 60983, 123451, 73289 };
        int[] areasSqrMiles                 = new int[] {  11818, 30956, 22642,  58519,   95543,  28541, 10739, 13240, 36214,   1889,
                                                           69306,  2169, 27585,   9551,   30347,   8632, 24564,  2172, 29166,  22153,
                                                           22365, 16356, 24800,   8049,    1541,  15294,  4511, 27730, 23546,  47665, 28297 };
        int[] ordersOfAdmissionToFederation = new int[] {      2,    14,     5,     16,      18,     31,    28,     4,     3,     27,
                                                              12,    24,    29,     13,       9,      1,    21,    23,    10,     20,
                                                              25,    30,    15,     26,      22,      8,    11,     7,     6,     17,    19 };
    }
    
    <h2 class="common-font bold text-center">United Mexican States</h2>
    
    <table border="6" class="common-font table-bordered" align="center">
        <tr>
            <td class="text-center">&nbsp;</td>
            <td>&nbsp;</td>
            <td colspan="2" class="text-center bold">Area</td>
            <td class="text-center bold">Admission to Federation</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td class="text-center short-text">#</td>
            <td class="bold">State Name</td>
            <td class="text-center bold">Sqr Kms</td>
            <td class="text-center bold">Sqr Miles</td>
            <td class="text-center bold">Order</td>
            <td class="bold">Capital</td>
        </tr>
        <tr>
            <td class="text-center">1</td><td>@states[0]</td><td class="text-right">@areasSqrKms[0]</td><td class="text-right">@areasSqrMiles[0]</td><td class="text-center">@ordersOfAdmissionToFederation[0]</td><td>@capitals[0]</td>
        </tr>
        <tr>
            <td class="text-center">2</td><td>@states[1]</td><td class="text-right">@areasSqrKms[1]</td><td class="text-right">@areasSqrMiles[1]</td><td class="text-center">@ordersOfAdmissionToFederation[1]</td><td>@capitals[1]</td>
        </tr>
        <tr>
            <td class="text-center">3</td><td>@states[2]</td><td class="text-right">@areasSqrKms[2]</td><td class="text-right">@areasSqrMiles[2]</td><td class="text-center">@ordersOfAdmissionToFederation[2]</td><td>@capitals[2]</td>
        </tr>
        <tr>
            <td class="text-center">4</td><td>@states[3]</td><td class="text-right">@areasSqrKms[3]</td><td class="text-right">@areasSqrMiles[3]</td><td class="text-center">@ordersOfAdmissionToFederation[3]</td><td>@capitals[3]</td>
        </tr>
        <tr>
            <td class="text-center">5</td><td>@states[4]</td><td class="text-right">@areasSqrKms[4]</td><td class="text-right">@areasSqrMiles[4]</td><td class="text-center">@ordersOfAdmissionToFederation[4]</td><td>@capitals[4]</td>
        </tr>
        <tr>
            <td class="text-center">6</td><td>@states[5]</td><td class="text-right">@areasSqrKms[5]</td><td class="text-right">@areasSqrMiles[5]</td><td class="text-center">@ordersOfAdmissionToFederation[5]</td><td>@capitals[5]</td>
        </tr>
        <tr>
            <td class="text-center">7</td><td>@states[6]</td><td class="text-right">@areasSqrKms[6]</td><td class="text-right">@areasSqrMiles[6]</td><td class="text-center">@ordersOfAdmissionToFederation[6]</td><td>@capitals[6]</td>
        </tr>
        <tr>
            <td class="text-center">8</td><td>@states[7]</td><td class="text-right">@areasSqrKms[7]</td><td class="text-right">@areasSqrMiles[7]</td><td class="text-center">@ordersOfAdmissionToFederation[7]</td><td>@capitals[7]</td>
        </tr>
        <tr>
            <td class="text-center">9</td><td>@states[8]</td><td class="text-right">@areasSqrKms[8]</td><td class="text-right">@areasSqrMiles[8]</td><td class="text-center">@ordersOfAdmissionToFederation[8]</td><td>@capitals[8]</td>
        </tr>
        <tr>
            <td class="text-center">10</td><td>@states[9]</td><td class="text-right">@areasSqrKms[9]</td><td class="text-right">@areasSqrMiles[9]</td><td class="text-center">@ordersOfAdmissionToFederation[9]</td><td>@capitals[9]</td>
        </tr>
        <tr>
            <td class="text-center">11</td><td>@states[10]</td><td class="text-right">@areasSqrKms[10]</td><td class="text-right">@areasSqrMiles[10]</td><td class="text-center">@ordersOfAdmissionToFederation[10]</td><td>@capitals[10]</td>
        </tr>
        <tr>
            <td class="text-center">12</td><td>@states[11]</td><td class="text-right">@areasSqrKms[11]</td><td class="text-right">@areasSqrMiles[11]</td><td class="text-center">@ordersOfAdmissionToFederation[11]</td><td>@capitals[11]</td>
        </tr>
        <tr>
            <td class="text-center">13</td><td>@states[12]</td><td class="text-right">@areasSqrKms[12]</td><td class="text-right">@areasSqrMiles[12]</td><td class="text-center">@ordersOfAdmissionToFederation[12]</td><td>@capitals[12]</td>
        </tr>
        <tr>
            <td class="text-center">14</td><td>@states[13]</td><td class="text-right">@areasSqrKms[13]</td><td class="text-right">@areasSqrMiles[13]</td><td class="text-center">@ordersOfAdmissionToFederation[13]</td><td>@capitals[13]</td>
        </tr>
        <tr>
            <td class="text-center">15</td><td>@states[14]</td><td class="text-right">@areasSqrKms[14]</td><td class="text-right">@areasSqrMiles[14]</td><td class="text-center">@ordersOfAdmissionToFederation[14]</td><td>@capitals[14]</td>
        </tr>
        <tr>
            <td class="text-center">16</td><td>@states[15]</td><td class="text-right">@areasSqrKms[15]</td><td class="text-right">@areasSqrMiles[15]</td><td class="text-center">@ordersOfAdmissionToFederation[15]</td><td>@capitals[15]</td>
        </tr>
        <tr>
            <td class="text-center">17</td><td>@states[16]</td><td class="text-right">@areasSqrKms[16]</td><td class="text-right">@areasSqrMiles[16]</td><td class="text-center">@ordersOfAdmissionToFederation[16]</td><td>@capitals[16]</td>
        </tr>
        <tr>
            <td class="text-center">18</td><td>@states[17]</td><td class="text-right">@areasSqrKms[17]</td><td class="text-right">@areasSqrMiles[17]</td><td class="text-center">@ordersOfAdmissionToFederation[17]</td><td>@capitals[17]</td>
        </tr>
        <tr>
            <td class="text-center">19</td><td>@states[18]</td><td class="text-right">@areasSqrKms[18]</td><td class="text-right">@areasSqrMiles[18]</td><td class="text-center">@ordersOfAdmissionToFederation[18]</td><td>@capitals[18]</td>
        </tr>
        <tr>
            <td class="text-center">20</td><td>@states[19]</td><td class="text-right">@areasSqrKms[19]</td><td class="text-right">@areasSqrMiles[19]</td><td class="text-center">@ordersOfAdmissionToFederation[19]</td><td>@capitals[19]</td>
        </tr>
        <tr>
            <td class="text-center">21</td><td>@states[20]</td><td class="text-right">@areasSqrKms[20]</td><td class="text-right">@areasSqrMiles[20]</td><td class="text-center">@ordersOfAdmissionToFederation[20]</td><td>@capitals[20]</td>
        </tr>
        <tr>
            <td class="text-center">22</td><td>@states[21]</td><td class="text-right">@areasSqrKms[21]</td><td class="text-right">@areasSqrMiles[21]</td><td class="text-center">@ordersOfAdmissionToFederation[21]</td><td>@capitals[21]</td>
        </tr>
        <tr>
            <td class="text-center">23</td><td>@states[22]</td><td class="text-right">@areasSqrKms[22]</td><td class="text-right">@areasSqrMiles[22]</td><td class="text-center">@ordersOfAdmissionToFederation[22]</td><td>@capitals[22]</td>
        </tr>
        <tr>
            <td class="text-center">24</td><td>@states[23]</td><td class="text-right">@areasSqrKms[23]</td><td class="text-right">@areasSqrMiles[23]</td><td class="text-center">@ordersOfAdmissionToFederation[23]</td><td>@capitals[23]</td>
        </tr>
        <tr>
            <td class="text-center">25</td><td>@states[24]</td><td class="text-right">@areasSqrKms[24]</td><td class="text-right">@areasSqrMiles[24]</td><td class="text-center">@ordersOfAdmissionToFederation[24]</td><td>@capitals[24]</td>
        </tr>
        <tr>
            <td class="text-center">26</td><td>@states[25]</td><td class="text-right">@areasSqrKms[25]</td><td class="text-right">@areasSqrMiles[25]</td><td class="text-center">@ordersOfAdmissionToFederation[25]</td><td>@capitals[25]</td>
        </tr>
        <tr>
            <td class="text-center">27</td><td>@states[26]</td><td class="text-right">@areasSqrKms[26]</td><td class="text-right">@areasSqrMiles[26]</td><td class="text-center">@ordersOfAdmissionToFederation[26]</td><td>@capitals[26]</td>
        </tr>
        <tr>
            <td class="text-center">28</td><td>@states[27]</td><td class="text-right">@areasSqrKms[27]</td><td class="text-right">@areasSqrMiles[27]</td><td class="text-center">@ordersOfAdmissionToFederation[27]</td><td>@capitals[27]</td>
        </tr>
        <tr>
            <td class="text-center">29</td><td>@states[28]</td><td class="text-right">@areasSqrKms[28]</td><td class="text-right">@areasSqrMiles[28]</td><td class="text-center">@ordersOfAdmissionToFederation[28]</td><td>@capitals[28]</td>
        </tr>
        <tr>
            <td class="text-center">30</td><td>@states[29]</td><td class="text-right">@areasSqrKms[29]</td><td class="text-right">@areasSqrMiles[29]</td><td class="text-center">@ordersOfAdmissionToFederation[29]</td><td>@capitals[29]</td>
        </tr>
        <tr>
            <td class="text-center">31</td><td>@states[30]</td><td class="text-right">@areasSqrKms[30]</td><td class="text-right">@areasSqrMiles[30]</td><td class="text-center">@ordersOfAdmissionToFederation[30]</td><td>@capitals[30]</td>
        </tr>
    </table>
    }
  13. To execute the application, on the main menu, click Debug -> Start Without Debugging:

    Payroll Preparation

  14. Return to your programming environment

Creating a Date Value

To let you create and manage dates, the .NET library provides a structure named DateOnly. Therefore, to create a date value, you can first declare a DateOnly variable. To assist you with initializing the variable, the DateOnly structure is equipped with three constructors. There is a default constructor. Its syntax is:

public DateOnly();

Presenting a Date Value

We have seen that one way to get a date value is to declare a DateOnly variable. In later sections, we will see other ways to get a date value. However you get that value, at some point, you may want to display it. The easiest way to display a date value is to use its variable. If the variable was declared in a razor page, you can put the variable in an HTML tag using the @ operator. Here is an example:

@page
@model Valuable.Pages.CreatureModel
@{
    DateOnly doStart = new DateOnly();
}

<h3>Dates Values</h3>
        
<p>Default date: @doStart</p>

<p>===========================================</p>

This would produce:

Dates Values

Default date: 1/1/0001

===========================================

Filling a Date

A date is a technique of identifying a period using three numbers: the year, the month, and the day. Each is an integer value. To create a date that holds such values, the DateOnly structure is equipped with a constructor that takes three integer arguments. Its syntax is:

public DateOnly (int year, int month, int day);

As you can see, this constructor takes three arguments: a value for the year, a value for the month, and a value for the day, respectively. Here is an example of creating a date object:

@page
@model Valuable.Pages.CreatureModel
@{
    DateOnly independance = new DateOnly(1960, 1, 1);
}

This would produce:

Independence Day: 1/1/1960
=======================================

Requesting a Date Value

If you are working on a webpage, if you want your users to provide a date value, you can create a simple text box (<input type"text" />). The problem is that user can type any value in that text box. To present a more professional solution, HTML provides a date-based control. To apply it, create an <input> control and set its type as date. Here are two examples:

<form name="frmBusiness" method="post">
    <table>
        <tr>
            <td>Date Hired:</td>
            <td><input type="date" id="dateHired" name="dateHired" /></td>
        </tr>
        <tr>
            <td>@Html.Label("txtOrientation", "Date Hired:", new { @class = "" })</td>
            <td>@Html.TextBox("txtOrientation", "", new { style = "width: 135px", type="date" })</td>
        </tr>
    </table>
</form>

As seen with Web controls so far, to use the value in your code, you can pass the name of the control to the double-quotes of Request.Form["control-name"].

Parsing a Date Value

When you request a value from a user, by default, that value is a string. If you are planning to use that value as a date, you may have to convert it first. To assist you with this operation, the DateOnly structure is equipped with the traditional Parse() method that we have used with other types. Here are examples of calling it:

@page
@model Valuable.Pages.CreatureModel
@{
    DateOnly doDateHired = new DateOnly();
    DateOnly doOrientatiopnDate = new DateOnly();

    if (Request.HasFormContentType)
    {
        doDateHired = DateOnly.Parse(Request.Form["txtDateHired"]);
        doOrientatiopnDate = DateOnly.Parse(Request.Form["txtOrientation"]);
    }
}

<form name="frmBusiness" method="post">
    <table>
        <tr>
            <td>Date Hired:</td>
            <td><input type="date" id="dateHired" name="dateHired" value=@doDateHired/></td>
        </tr>
        <tr>
            <td>@Html.Label("txtOrientation", "Date Hired:", new { @class = "" })</td>
            <td>@Html.TextBox("txtOrientation", @doOrientatiopnDate, new { style = "width: 135px", type="date" })</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" value="Validate the Date" name="btnValidate" /></td>
        </tr>
    </table>
</form>

Converting a String to Date

Before displaying a date value, you can first convert that value to a string. To support this, the DateOnly structure overrides the ToString() method that is overloaded with four versions. One of the versions takes no argument and its syntax is:

public override string ToString();

If you call this version of the method, the compiler uses a default format depending on the language set on the user's computer. Here is an example:

@page
@model Valuable.Pages.CreatureModel
@using static System.Console
@{
    DateOnly doDateHired = new DateOnly();
    DateOnly doOrientationDate = new DateOnly();

    if (Request.HasFormContentType)
    {
        doDateHired = DateOnly.Parse(Request.Form["txtDateHired"]);
        doOrientationDate = DateOnly.Parse(Request.Form["txtOrientation"]);
        
        WriteLine("Date Hired:       " + @doDateHired.ToString());
        WriteLine("Orientation Date: " + @doOrientationDate.ToString());
    }
}

<form name="frmBusiness" method="post">
    <table>
        <tr>
            <td>Date Hired:</td>
            <td><input type="date" name="txtDateHired" value="@doDateHired" /></td>
        </tr>
        <tr>
            <td>@Html.Label("txtOrientation", "Orientation Date:", new { @class = "" })</td>
            <td>@Html.TextBox("txtOrientation", @doOrientationDate, new { style = "width: 135px", type="date" })</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" value="Validate the Date" name="btnValidate" /></td>
        </tr>
    </table>

    <p>Date Hired: @doDateHired.ToString()</p>
    <p>Date Hired: @doOrientationDate.ToString()</p>
</form>

If you want to control how the date should be rendered, you can use the version of the ToString() method that takes as argument a string value.

Formatting a String for a Date

As you may know already, the string data type is equipped with the overloaded Format() method that allows you to control the content of a string. In the first argument of this method, you can create one or more {} placeholders to specify how the value should appear.

String Interpolation

String interpolation is another technique used to format a string. You use it by starting a string with $ and including the traditional double-quotes. In the quotes, create the neccessary {} placeholders. In a placeholder, type the name of a DateOnly variable, a colon, and a format.

A Date as a Type

Introduction

As we will see in various examples, the DateOnly type is used like any structure or type. Besides using it to declare variables, you can pass it as argument to a function or method and you can return its value from a function or method.

A Field of a Date Type

You can create a field that is a date/time type as a member of a class. You can declare the variable in the body of the class and initialize it in a method of the class. The initialization is typically done in a constructor. Here is an example:

@functions{
    public class Employee
    {
        private DateOnly dtHired;

        public State()
        {
            dt = new DateOnly();
        }
    }
}

A Date Property

A property of DateOnly type is created like that of a structure. Whenever you need to initialize it or give it a value, proceed as for any structure.

To create a comple date property, you can first declare a private DateOnly field in a class, record, or structure. Then, in the body of the class, record, or structure, create the property with a get and a set/init clauses. Here is an example:

internal record Purchase
{
    DateOnly dDay;

    public DateOnly Date
    {
        get { return dDay; }
        init { dDay = value; }
    }
}

In the same way, you can create a read-only or an automatic property. In fact, you can create the property in a page model. Here is an example:

using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Valuable.Pages
{
    public class ArraysModel : PageModel
    {
        public DateOnly DateHired { get; set; }

        public ArraysModel()
        {
            DateHired = new DateOnly(2020, 8, 16);
        }

        public void OnGet()
        {
        }
    }
}

Either way, after creating the property, you can access it. If you had created the property in a class created in its own code file or in a razor page, you can declare a variable of the class and access the property. If you had created the property in a page model, to access that property in a razor page, use the @Model object. Here is an example:

@page
@model Valuable.Pages.ArraysModel
@{   
}

<p>Date Hired: @Model.DateHired</p>

An Array of Date Values

To create an array of date values, declare the DateOnly variable as an array. To initialize the array, you can provide a value for each item using a constructor of the DateOnly structure. Here is an example:

@{
    DateOnly[] dateOfAdmissionToFederation = new DateOnly[]
    {
        new DateOnly(1823, 12, 20),
        new DateOnly(1824,  2,  7),
        new DateOnly(1823, 12, 22),
        new DateOnly(1824,  5,  7)
    };
}

After creating the array, you can access its elements. You can access one element at a time or you can loop through the array.

Practical LearningPractical Learning: Presenting an Array of Date Values

  1. Change the Index.cshtml razor page as follows:
    @page
    @model IndexModel
    @{
        string[] states = new string[31];
    
        states[0]  = "Guanajuato";      states[1]  = "Tamaulipas";          states[2]  = "Michoacán";  states[3]  = "Coahuila";
        states[4]  = "Chihuahua";       states[5]  = "Baja California Sur"; states[6]  = "Nayarit";    states[7]  = "Puebla";
        states[8]  = "Oaxaca";          states[9]  = "Morelos";             states[10] = "Sonora";     states[11] = "Aguascalientes";
        states[12] = "Baja California"; states[13] = "Tabasco";             states[14] = "Jalisco";    states[15] = "México";
        states[16] = "Guerrero";        states[17] = "Colima";              states[18] = "Zacatecas";  states[19] = "Sinaloa";
        states[20] = "Campeche";        states[21] = "Quintana Roo";        states[22] = "Nuevo León"; states[23] = "Hidalgo";
        states[24] = "Tlaxcala";        states[25] = "Yucatán";             states[26] = "Querétaro";  states[27] = "Veracruz";
        states[28] = "San Luis Potosí"; states[29] = "Durango";             states[30] = "Chiapas";
    
        string[] capitals = new string[] { "Guanajuato",                "Ciudad Victoria",           "Morelia",               "Saltillo",         "Chihuahua",
                                           "La Paz",                    "Tepic",                     "Puebla de Zaragoza",    "Oaxaca de Juárez", "Cuernavaca",
                                           "Hermosillo",                "Aguascalientes",            "Mexicali",              "Villahermosa",     "Guadalajara",
                                           "Toluca de Lerdo",           "Chilpancingo de los Bravo", "Colima",                "Zacatecas",        "Culiacán",
                                           "San Francisco de Campeche", "Chetumal",                  "Monterrey",             "Pachuca",
                                           "Tlaxcala",                  "Mérida",                    "Santiago de Querétaro", "Xalapa",
                                           "San Luis Potosí",           "Victoria de Durango",       "Tuxtla Gutiérrez"};
        int[] areasSqrKms                   = new int[] {  30608, 80175, 58643, 151563,  247455,  73922, 27815, 34290, 93793,   4893,
                                                          179503,  5618, 71446,  24738,   78599,  22357, 63621,  5625, 75539,  57377,
                                                           57924, 42361, 64220,  20846,    3991,  39612, 11684, 71820, 60983, 123451, 73289 };
        int[] areasSqrMiles                 = new int[] {  11818, 30956, 22642,  58519,   95543,  28541, 10739, 13240, 36214,   1889,
                                                           69306,  2169, 27585,   9551,   30347,   8632, 24564,  2172, 29166,  22153,
                                                           22365, 16356, 24800,   8049,    1541,  15294,  4511, 27730, 23546,  47665, 28297 };
        int[] ordersOfAdmissionToFederation = new int[] {      2,    14,     5,     16,      18,     31,    28,     4,     3,     27,
                                                              12,    24,    29,     13,       9,      1,    21,    23,    10,     20,
                                                              25,    30,    15,     26,      22,      8,    11,     7,     6,     17,    19 };
    
        DateOnly[] dateOfAdmissionToFederation = new DateOnly[]
        {
            new DateOnly(1823, 12, 20), new DateOnly(1824,  2,  7), new DateOnly(1823, 12, 22), new DateOnly(1824,  5,  7),
            new DateOnly(1824,  7,  6), new DateOnly(1952,  1, 16), new DateOnly(1917,  1, 26), new DateOnly(1823, 12, 21),
            new DateOnly(1823, 12, 21), new DateOnly(1869,  4, 17), new DateOnly(1824,  1, 10), new DateOnly(1857,  2,  5),
            new DateOnly(1952,  1, 16), new DateOnly(1824,  2,  7), new DateOnly(1823, 12, 23), new DateOnly(1823, 12, 20),
            new DateOnly(1849, 10, 27), new DateOnly(1856,  9, 12), new DateOnly(1823, 12, 23), new DateOnly(1830, 10, 14),
            new DateOnly(1863,  4, 29), new DateOnly(1974, 10,  8), new DateOnly(1824,  5,  7), new DateOnly(1869,  1, 16),
            new DateOnly(1856, 12,  9), new DateOnly(1823, 12, 23), new DateOnly(1823, 12, 23), new DateOnly(1823, 12, 22),
            new DateOnly(1823, 12, 22), new DateOnly(1824,  5, 22), new DateOnly(1824,  9, 14)
        };
    }
    
    <h2 class="common-font bold text-center">United Mexican States</h2>
    
    <table border="6" class="common-font table-bordered" align="center">
        <tr>
            <td class="text-center" style="width: 40px">&nbsp;</td>
            <td style="width: 150px">&nbsp;</td>
            <td colspan="2" class="text-center bold">Area</td>
            <td class="text-center bold" colspan="2">Admission to Federation</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td class="text-center short-text">#</td>
            <td class="bold">State Name</td>
            <td class="text-center bold">Sqr Kms</td>
            <td class="text-center bold">Sqr Miles</td>
            <td class="text-center bold">Order</td>
            <td class="text-center bold">Date</td>
            <td class="bold">Capital</td>
        </tr>
        <tr>
            <td class="text-center">1</td><td>@states[0]</td><td class="text-right">@areasSqrKms[0]</td><td class="text-right">@areasSqrMiles[0]</td><td class="text-center">@ordersOfAdmissionToFederation[0]</td><td class="text-center">@dateOfAdmissionToFederation[0]</td><td>@capitals[0]</td>
        </tr>
        <tr>
            <td class="text-center">2</td><td>@states[1]</td><td class="text-right">@areasSqrKms[1]</td><td class="text-right">@areasSqrMiles[1]</td><td class="text-center">@ordersOfAdmissionToFederation[1]</td><td class="text-center">@dateOfAdmissionToFederation[1]</td><td>@capitals[1]</td>
        </tr>
        <tr>
            <td class="text-center">3</td><td>@states[2]</td><td class="text-right">@areasSqrKms[2]</td><td class="text-right">@areasSqrMiles[2]</td><td class="text-center">@ordersOfAdmissionToFederation[2]</td><td class="text-center">@dateOfAdmissionToFederation[2]</td><td>@capitals[2]</td>
        </tr>
        <tr>
            <td class="text-center">4</td><td>@states[3]</td><td class="text-right">@areasSqrKms[3]</td><td class="text-right">@areasSqrMiles[3]</td><td class="text-center">@ordersOfAdmissionToFederation[3]</td><td class="text-center">@dateOfAdmissionToFederation[3]</td><td>@capitals[3]</td>
        </tr>
        <tr>
            <td class="text-center">5</td><td>@states[4]</td><td class="text-right">@areasSqrKms[4]</td><td class="text-right">@areasSqrMiles[4]</td><td class="text-center">@ordersOfAdmissionToFederation[4]</td><td class="text-center">@dateOfAdmissionToFederation[4]</td><td>@capitals[4]</td>
        </tr>
        <tr>
            <td class="text-center">6</td><td>@states[5]</td><td class="text-right">@areasSqrKms[5]</td><td class="text-right">@areasSqrMiles[5]</td><td class="text-center">@ordersOfAdmissionToFederation[5]</td><td class="text-center">@dateOfAdmissionToFederation[5]</td><td>@capitals[5]</td>
        </tr>
        <tr>
            <td class="text-center">7</td><td>@states[6]</td><td class="text-right">@areasSqrKms[6]</td><td class="text-right">@areasSqrMiles[6]</td><td class="text-center">@ordersOfAdmissionToFederation[6]</td><td class="text-center">@dateOfAdmissionToFederation[6]</td><td>@capitals[6]</td>
        </tr>
        <tr>
            <td class="text-center">8</td><td>@states[7]</td><td class="text-right">@areasSqrKms[7]</td><td class="text-right">@areasSqrMiles[7]</td><td class="text-center">@ordersOfAdmissionToFederation[7]</td><td class="text-center">@dateOfAdmissionToFederation[7]</td><td>@capitals[7]</td>
        </tr>
        <tr>
            <td class="text-center">9</td><td>@states[8]</td><td class="text-right">@areasSqrKms[8]</td><td class="text-right">@areasSqrMiles[8]</td><td class="text-center">@ordersOfAdmissionToFederation[8]</td><td class="text-center">@dateOfAdmissionToFederation[8]</td><td>@capitals[8]</td>
        </tr>
        <tr>
            <td class="text-center">10</td><td>@states[9]</td><td class="text-right">@areasSqrKms[9]</td><td class="text-right">@areasSqrMiles[9]</td><td class="text-center">@ordersOfAdmissionToFederation[9]</td><td class="text-center">@dateOfAdmissionToFederation[9]</td><td>@capitals[9]</td>
        </tr>
        <tr>
            <td class="text-center">11</td><td>@states[10]</td><td class="text-right">@areasSqrKms[10]</td><td class="text-right">@areasSqrMiles[10]</td><td class="text-center">@ordersOfAdmissionToFederation[10]</td><td class="text-center">@dateOfAdmissionToFederation[10]</td><td>@capitals[10]</td>
        </tr>
        <tr>
            <td class="text-center">12</td><td>@states[11]</td><td class="text-right">@areasSqrKms[11]</td><td class="text-right">@areasSqrMiles[11]</td><td class="text-center">@ordersOfAdmissionToFederation[11]</td><td class="text-center">@dateOfAdmissionToFederation[11]</td><td>@capitals[11]</td>
        </tr>
        <tr>
            <td class="text-center">13</td><td>@states[12]</td><td class="text-right">@areasSqrKms[12]</td><td class="text-right">@areasSqrMiles[12]</td><td class="text-center">@ordersOfAdmissionToFederation[12]</td><td class="text-center">@dateOfAdmissionToFederation[12]</td><td>@capitals[12]</td>
        </tr>
        <tr>
            <td class="text-center">14</td><td>@states[13]</td><td class="text-right">@areasSqrKms[13]</td><td class="text-right">@areasSqrMiles[13]</td><td class="text-center">@ordersOfAdmissionToFederation[13]</td><td class="text-center">@dateOfAdmissionToFederation[13]</td><td>@capitals[13]</td>
        </tr>
        <tr>
            <td class="text-center">15</td><td>@states[14]</td><td class="text-right">@areasSqrKms[14]</td><td class="text-right">@areasSqrMiles[14]</td><td class="text-center">@ordersOfAdmissionToFederation[14]</td><td class="text-center">@dateOfAdmissionToFederation[14]</td><td>@capitals[14]</td>
        </tr>
        <tr>
            <td class="text-center">16</td><td>@states[15]</td><td class="text-right">@areasSqrKms[15]</td><td class="text-right">@areasSqrMiles[15]</td><td class="text-center">@ordersOfAdmissionToFederation[15]</td><td class="text-center">@dateOfAdmissionToFederation[15]</td><td>@capitals[15]</td>
        </tr>
        <tr>
            <td class="text-center">17</td><td>@states[16]</td><td class="text-right">@areasSqrKms[16]</td><td class="text-right">@areasSqrMiles[16]</td><td class="text-center">@ordersOfAdmissionToFederation[16]</td><td class="text-center">@dateOfAdmissionToFederation[16]</td><td>@capitals[16]</td>
        </tr>
        <tr>
            <td class="text-center">18</td><td>@states[17]</td><td class="text-right">@areasSqrKms[17]</td><td class="text-right">@areasSqrMiles[17]</td><td class="text-center">@ordersOfAdmissionToFederation[17]</td><td class="text-center">@dateOfAdmissionToFederation[17]</td><td>@capitals[17]</td>
        </tr>
        <tr>
            <td class="text-center">19</td><td>@states[18]</td><td class="text-right">@areasSqrKms[18]</td><td class="text-right">@areasSqrMiles[18]</td><td class="text-center">@ordersOfAdmissionToFederation[18]</td><td class="text-center">@dateOfAdmissionToFederation[18]</td><td>@capitals[18]</td>
        </tr>
        <tr>
            <td class="text-center">20</td><td>@states[19]</td><td class="text-right">@areasSqrKms[19]</td><td class="text-right">@areasSqrMiles[19]</td><td class="text-center">@ordersOfAdmissionToFederation[19]</td><td class="text-center">@dateOfAdmissionToFederation[19]</td><td>@capitals[19]</td>
        </tr>
        <tr>
            <td class="text-center">21</td><td>@states[20]</td><td class="text-right">@areasSqrKms[20]</td><td class="text-right">@areasSqrMiles[20]</td><td class="text-center">@ordersOfAdmissionToFederation[20]</td><td class="text-center">@dateOfAdmissionToFederation[20]</td><td>@capitals[20]</td>
        </tr>
        <tr>
            <td class="text-center">22</td><td>@states[21]</td><td class="text-right">@areasSqrKms[21]</td><td class="text-right">@areasSqrMiles[21]</td><td class="text-center">@ordersOfAdmissionToFederation[21]</td><td class="text-center">@dateOfAdmissionToFederation[21]</td><td>@capitals[21]</td>
        </tr>
        <tr>
            <td class="text-center">23</td><td>@states[22]</td><td class="text-right">@areasSqrKms[22]</td><td class="text-right">@areasSqrMiles[22]</td><td class="text-center">@ordersOfAdmissionToFederation[22]</td><td class="text-center">@dateOfAdmissionToFederation[22]</td><td>@capitals[22]</td>
        </tr>
        <tr>
            <td class="text-center">24</td><td>@states[23]</td><td class="text-right">@areasSqrKms[23]</td><td class="text-right">@areasSqrMiles[23]</td><td class="text-center">@ordersOfAdmissionToFederation[23]</td><td class="text-center">@dateOfAdmissionToFederation[23]</td><td>@capitals[23]</td>
        </tr>
        <tr>
            <td class="text-center">25</td><td>@states[24]</td><td class="text-right">@areasSqrKms[24]</td><td class="text-right">@areasSqrMiles[24]</td><td class="text-center">@ordersOfAdmissionToFederation[24]</td><td class="text-center">@dateOfAdmissionToFederation[24]</td><td>@capitals[24]</td>
        </tr>
        <tr>
            <td class="text-center">26</td><td>@states[25]</td><td class="text-right">@areasSqrKms[25]</td><td class="text-right">@areasSqrMiles[25]</td><td class="text-center">@ordersOfAdmissionToFederation[25]</td><td class="text-center">@dateOfAdmissionToFederation[25]</td><td>@capitals[25]</td>
        </tr>
        <tr>
            <td class="text-center">27</td><td>@states[26]</td><td class="text-right">@areasSqrKms[26]</td><td class="text-right">@areasSqrMiles[26]</td><td class="text-center">@ordersOfAdmissionToFederation[26]</td><td class="text-center">@dateOfAdmissionToFederation[26]</td><td>@capitals[26]</td>
        </tr>
        <tr>
            <td class="text-center">28</td><td>@states[27]</td><td class="text-right">@areasSqrKms[27]</td><td class="text-right">@areasSqrMiles[27]</td><td class="text-center">@ordersOfAdmissionToFederation[27]</td><td class="text-center">@dateOfAdmissionToFederation[27]</td><td>@capitals[27]</td>
        </tr>
        <tr>
            <td class="text-center">29</td><td>@states[28]</td><td class="text-right">@areasSqrKms[28]</td><td class="text-right">@areasSqrMiles[28]</td><td class="text-center">@ordersOfAdmissionToFederation[28]</td><td class="text-center">@dateOfAdmissionToFederation[28]</td><td>@capitals[28]</td>
        </tr>
        <tr>
            <td class="text-center">30</td><td>@states[29]</td><td class="text-right">@areasSqrKms[29]</td><td class="text-right">@areasSqrMiles[29]</td><td class="text-center">@ordersOfAdmissionToFederation[29]</td><td class="text-center">@dateOfAdmissionToFederation[29]</td><td>@capitals[29]</td>
        </tr>
        <tr>
            <td class="text-center">31</td><td>@states[30]</td><td class="text-right">@areasSqrKms[30]</td><td class="text-right">@areasSqrMiles[30]</td><td class="text-center">@ordersOfAdmissionToFederation[30]</td><td class="text-center">@dateOfAdmissionToFederation[30]</td><td>@capitals[30]</td>
        </tr>
    </table>
    }
  2. To execute the application, on the main menu, click Debug -> Start Without Debugging:

    Dates and Times Values

  3. Return to your programming environment

Introduction to the Characteristics of a Date Value

Introduction

The pieces of information of the DateOnly structure are:

The Default Date Values

The default constructor of the DateOnly structure initializes the date to January 1st, 0001 and the time at midnight (01:01:01). Here is an example that shows it:

@page
@model Exercise.Pages.ExerciseModel
@{
    DateOnly tm = new DateOnly();
}
        
<h3>Dates Values</h3>
        
<p>Default date and time: @tm</p>

<p>===========================================</p>

This would produce:

Default date and time: 1/1/0001
===========================================

The Minimum Date Value

This also means that the lowest Dates Values that a DateOnly object can hold is January 1st, 0001 at 00:00:00. This value is represented by the MinValue constant member of the DateOnly structure.

The Maximum Date Value

The highest date and time that a DateOnly object can hold in the structure is called MaxValue and it is set at December 31, 9999.

Dates Parts

Introduction to Rules of Date Formats

The computer uses two main categories to display dates using specific characters to represent its value.

MM

The double M (in uppercase) string gets the numeric value of the month from 1 to 12. If the number is less than 10, it would display with the leading 0. Here is an example:

@page
@model Exercise.Pages.ExerciseModel
@{
    DateOnly date = new DateOnly(2022, 4, 22);
    string strMonth = date.ToString("MM");
}

<h3>Dates Characteristics</h3>

<p>Date: @date</p> 

<p>Month Value:  @strMonth</p>

<p>===========================================</p>

This would produce:

Date: 4/22/2022
Month Value:   04
===========================================

If the number is higher than 9, it would display so.

MMM

The triple M as MMM (in uppercase) gets the name of the month using three letters. This variable is defined by the operating system. The names of the month in US English are Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, and Dec. Here is an example:

@page
@model Exercise.Pages.ExerciseModel
@{
    DateOnly date = new DateOnly(1998, 1, 12);
    string strMonth = date.ToString("MMM");
}

<h3>Dates Characteristics</h3>

<p>Date: @date</p>

<p>Month Name: @strMonth</p>

<p>===========================================</p>

This would display:

Date and Time: 1/12/1998
Month Name: Jan
===========================================

MMMM

The quadruple M as MMMM (in uppercase) gets the complete name of a month as defined by the operating system of the user's computer. The names of the months are January, February, March, April, May, June, July, August, September, October, November, and December. Here is an example:

@page
@model Exercise.Pages.ExerciseModel
@{
        DateOnly date = new DateOnly(2020, 10, 23);
        string strMonth = date.ToString("MMMM");
}

<h3>Dates Characteristics</h3>

<p>Date: @date</p>

<p>Month Name: @strMonth</p>

<p>===========================================</p>

This would produce:

Date and Time: 10/23/2020
Month Name:    October
===========================================

dd

The double d gets the numeric day of the month. If the number is less than 10, it would display with a leading 0. Here is an example:

@page
@model Exercise.Pages.ExerciseModel
@{
    DateOnly date = new DateOnly(2022, 4, 2);
    string strDay = date.ToString("dd");
}

<h3>Dates Characteristics</h3>

<p>Date: @date</p>
<p>Day Value: @strDay</p>
<p>===========================================</p>

This would display:

Date and Time: 4/2/2022
Day Value:   02
===========================================

yy

The double y is used to get the numeric year with the last two digits. Here is an example:

@page
@model Exercise.Pages.ExerciseModel
@{
    DateOnly date = new DateOnly(2022, 4, 2);
    string strYear2Digits = date.ToString("yy");
}
<h3>Dates Characteristics</h3>

<p>Date: @date</p>

<p>Year Value: @strYear2Digits</p>

<p>===========================================</p>

This would display:

Date and Time: 4/2/2022
Year Value:    02
===========================================

yyyy

The yyyy string is used to get all four digits of a year. Here is an example:

@page
@model Exercise.Pages.ExerciseModel
@{
    DateOnly date = new DateOnly(2022, 4, 2);
    string strYear4Digits = date.ToString("yyyy");
}

<h3>Dates Characteristics</h3>

<p>Date: @date</p>

<p>Year Value:    @strYear4Digits</p>

<p>===========================================</p>

This would display:

Date and Time: 4/2/2022
Year Value:    2002
===========================================

If the year was provided with two digits, such as 98, it would still be produced with 4 digits. Consider the following example:

@page
@model Exercise.Pages.ExerciseModel
@{
    DateOnly date = new DateOnly(98, 4, 2);
    string strYear4Digits = date.ToString("yyyy");
}

<h3>Dates Characteristics</h3>

<p>Date: @date</p>

<p>Year Value:    @strYear4Digits</p>

<p>===========================================</p>

This would display:

Date and Time: 4/2/0098
Year Value:    0098
===========================================

Notice that this may be a wrong date. For this reason, in your C# applications, you should always make it a habit to (always) provide your years with 4 digits.

Date Formats

Getting a Date Value From a DateOnly Object

You may have noticed that, by default, a DateOnly object always produces both a date and a time. In some cases, you will be interested in only the date portion of the object. To get a date value, you can call the DateOnly.ToString() method that takes a string as argument and apply some rules:

Empty Space

Between the components of a date value, you are allowed to leave empty spaces if you want. Don't pass an empty space to the ToString() method.

The Comma: ,

To separate the sections of a date value, you can use the comma. Don't pass a comma by itself to the ToString() method.

Date Separator,

The compiler refers to the Control Panel to identify this character. In US English, the forward slash is used to separate the portions of a date:

Customize Regional Options

Don't pass the forward slash by itself to the ToString() method.

Dash and Others: - .

Besides the forward slash, the user's computer may allow other characters. For example, in US English, the "-" can be used. You can check available characters in the Date Separator combo box of the Date tab of the Customize Regional Options of the Control Panel. Don't pass any of these characters by itself to the ToString() method.

The other characters and their combinations (MM, MMM, MMMM, dd, yy, and yyyy) are used as we reviewed them.

Here are examples of displaying date formats:

@page
@model Exercise.Pages.ExerciseModel
@{
    DateOnly date = new DateOnly(2004, 10, 23);
}

<h3>Dates Characteristics</h3>

<pre>Date Value: @date.ToString("M/d/yyyy")
Date Value: @date.ToString("M/d/yy"
Date Value: @date.ToString("MM/dd/yy"
Date Value: @date.ToString("MM/dd/yyyy"
Date Value: @date.ToString("yy/MM/dd"
Date Value: @date.ToString("yyyy-MM-dd"
Date Value: @date.ToString("dd-MMM-yy"
=================================</pre>

This would produce:

Date Value: 10/23/2004
Date Value: 10/23/04
Date Value: 10/23/04
Date Value: 10/23/2004
Date Value: 04/10/23
Date Value: 2004-10-23
Date Value: 23-Oct-04
=================================

The Short Date

A date is referred to as short if it includes (only) the numeric portions of the month and the day of a date value. To support short dates, the DateOnly structure is equipped with a method named ToShortDateString. Its syntax is:

public string ToShortDateString();

As another option to get a short date, on a DateOnly value, pass a "d" (one d in lowercase) string to the ToString() method. Here is an example:

@page
@model Exercise.Pages.ExerciseModel
@{
    DateOnly date = new DateOnly(2024, 10, 23);
    string strDate = date.ToString("d");
}

<h3>Dates Characteristics</h3>

<pre>Date: @date
Date:  @strDate
=====================================</pre>

This would produce:

Dates Characteristics

Date: 10/23/2024
Date: 10/23/2024
===========================================

The Long Date Format

A date is referred to as long if it includes the names of the month and of the day of the week of a date value. This is called the Long Date Format. To let you get the Long Date of a date, the DateOnly structure is equipped with a method named ToLongDateString, Its syntax is:

public string ToLongDateString ();

As another way to get a long date, pass a "D" (one d in uppercase) string to the ToString() method of a DateOnly value. Here is an example:

@page
@model Exercise.Pages.ExerciseModel
@{
    DateOnly date = new DateOnly(2024, 10, 23);
    string strDate = date.ToString("D");
}

<h3>Dates Characteristics</h3>

<pre>Date and Time: @date
Date Portion:  @strDate
===========================================</pre>

This would produce:

Dates Characteristics

Date: 10/23/2024
Date:  Wednesday, October 23, 2024
===========================================

Other Date Formats

The .NET library provides other formats, not regularly used but available. To get the name of a month and the year value of a DateOnly object, both separated by an empty space, pass a single M (uppercase) as string to the ToString() method of a DateOnly object. Here is an example:

@page
@model Exercise.Pages.ExerciseModel
@{
    DateOnly date = new DateOnly(2025, 10, 23);
    string strDate = date.ToString("M");
}

<h3>Dates Characteristics</h3>

<pre>Date and Time:  @date

Month and Year: @strDate
=======================================</pre>

This would produce:

Date and Time:  10/23/2025
Month and Year: October 23
=======================================

To include a comma in the result, pass a single y (lowercase) as string to the ToString() method of a DateOnly object. Here is an example:

@page
@model Exercise.Pages.ExerciseModel
@{
    DateOnly date = new DateOnly(2026, 10, 23);
    string strDate = date.ToString("y");
}

<h3>Dates Characteristics</h3>

<pre>Date:  @date
Month and Year: @strDate
=======================================</pre>

This would produce:

Date:  10/23/2026
Month and Year: October, 2004
=======================================

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2001-2022, FunctionX Thursday 14 October 2021 Next