Introduction to Date Values
Introduction to Date Values
Fundamentals of Dates
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. The .NET Framework compilers count the years 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 Learning: Introducing Date/Time Values
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace CountriesStatistics08.Controllers { public class MexicoController : Controller { // GET: Mexico public ActionResult Index() { return View(); } // GET: States public ActionResult States() { return View(); } // GET: StateSearch public ActionResult StateSearch() { return View(); } } }
@{ ViewBag.Title = "United Mexican States"; } <h2>United Mexican States</h2>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CountriesStatistics08.Models { interface IAbbreviated { string Abbreviation { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace CountriesStatistics08.Models { public abstract class GovernmentEntity { public virtual string StateName { get; set; } virtual public int AreaSqrKms { get; set; } public virtual string Capital { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace CountriesStatistics08.Models { public class State : GovernmentEntity, IAbbreviated { public string Abbreviation { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace CountriesStatistics08.Models { public class Federation : State { public int AdmissionUnionOrder { get; set; } } }
A Date Variable
To represent a date, the Microsoft Windows operating systems provide a structure named SYSTEMTIME. To let you create and manage dates, the .NET Framework provides a structure named DateTime. To create a date or a time value, you can first declare DateTime variable. To assist you with initializing the variable, the DateTime structure is equipped with various constructors.
The default constructor allows you to create a date or a time object without specifying its details. This would be done as follows:
@{
DateTime date = new DateTime();
}
After declaring a DateTime variable and initializing it, it holds all necessary pieces of information about its date and its time values.
Creating a Date Value
A date is a technique of identifying a period using three numbers: the year, the month, and the day. Each is an integer value. If you know the values you want to use to create or specify a date value, when declaring a DateTime variable, you can use the following constructor:
public DateTime(int year, int month, int day);
Here is an example of creating a date:
<!DOCTYPE html>
<html>
<head>
<title>Date Values</title>
</head>
<body>
<h1>Date Values</h1>
@{
DateTime independance = new DateTime(1960, 1, 1);
}
</body>
</html>
Converting a String to Date
Before displaying a DateTime value in a webpage, you can first convert it to a string. To support this, the DateTime structure overrides the ToString() method that is overloaded with various 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. 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.
A Date as a Type
Introduction
As we will see in various examples, the DateTime type is used like any structure or type. Besides using it to declare variables, you can pass it as argument to method and you can return its value from a method.
A Date Field
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:
using System;
public class Employee
{
private DateTime dtHired;
public State()
{
dt = new DateTime(2018, 5, 26);
}
}
A Date Property
A property of DateTime type is created like that of a structure. Whenever you need to initialize it or give it a value, proceed as for any structure.
Practical Learning: Creating a Date-Based Property
namespace CountriesStatistics08.Models
{
public class Federation : State
{
public int AdmissionUnionOrder { get; set; }
public DateTime AdmissionUnionDate { get; set; }
}
}
An Array of Date Values
To create an array of dates, times, or date and time combinations, declare the variable as an array. To initialize the array, you can provide a value for each item using a constructor of the DateTime structure.
Practical Learning: Creating an Array of Date Values
@{
ViewBag.Title = "United Mexican States";
}
<h2>United Mexican States</h2>
@{
DateTime[] dateOfAdmissionToFederation = new DateTime[]
{
new DateTime(1823, 12, 20), new DateTime(1824, 2, 7), new DateTime(1823, 12, 22), new DateTime(1824, 5, 7),
new DateTime(1824, 7, 6), new DateTime(1952, 1, 16), new DateTime(1917, 1, 26), new DateTime(1823, 12, 21),
new DateTime(1823, 12, 21), new DateTime(1869, 4, 17), new DateTime(1824, 1, 10), new DateTime(1857, 2, 5),
new DateTime(1952, 1, 16), new DateTime(1824, 2, 7), new DateTime(1823, 12, 23), new DateTime(1823, 12, 20),
new DateTime(1849, 10, 27), new DateTime(1856, 9, 12), new DateTime(1823, 12, 23), new DateTime(1830, 10, 14),
new DateTime(1863, 4, 29), new DateTime(1974, 10, 8), new DateTime(1824, 5, 7), new DateTime(1869, 1, 16),
new DateTime(1856, 12, 9), new DateTime(1823, 12, 23), new DateTime(1823, 12, 23), new DateTime(1823, 12, 22),
new DateTime(1823, 12, 22), new DateTime(1824, 5, 22), new DateTime(1824, 9, 14)
};
}
Primary Characteristics of a Date Value
The Default Values of a Date
The default constructor of the DateTime structure initializes the date to January 1st, 0001. Here is an example that shows it:
<!DOCTYPE html> <html> <head> <title>Date and Time Values</title> </head> <body> <h1>Date and Time Values</h1> @{ DateTime tm = new DateTime(); } </body> </html>
The Minimum Date Value
The lowest date value that a DateTime object can hold is January 1st, 0001. This value is represented by the MinValue constant member of the DateTime structure.
The Maximum Date Value
The highest date value that a DateTime object can hold in the structure is called MaxValue and it is set at December 31, 9999.
Requesting and Presenting a Date Value
A Text Box to Request a Date Value
Normally, to request a date from the visitor of a webpage, you can create a text box. As one option, use the caption of the text box to indicate to the user the format to use. Here is an example:
@{
ViewBag.Title = "Exercise";
}
<h2>Exercise</h2>
<form name="EmploymmentApplication">
<table>
<tr>
<td><label for="dateHired">Date Hired (MM/DD/YYYY):</label></td>
<td><input type="text" id="dateHired" /></td>
</tr>
</table>
</form>
This would produce:
As another way you can assist the user, you can add a placeholder attribute to an input control of type text. You can set the value of the placeholder attribute to indicate the format the user should use. Here is an example:
<form name="EmploymmentApplication">
<table>
<tr>
<td><label for="dateHired">Date Hired:</label></td>
<td><input type="date" id="dateHired" placeholder="MM/DD/YYYY" /></td>
</tr>
</table>
</form>
This would produce:
The visitor can then click in the text box, which would remove the text of the placeholder, and start typing.
A Text Box to Present a Date Value
It depends on how you want to present a date to a visitore of your webpage. To present a value that a user can change, you can create a text box. Set the value attribute appropriate, such as the name of a DateTime variable. Here is an example:
@{
ViewBag.Title = "Exercise";
}
<h2>Exercise</h2>
@{
DateTime dateHired = new DateTime(2019, 04, 17);
}
<form name="EmploymmentApplication">
<table>
<tr>
<td><label for="dateHired">Date Hired:</label></td>
<td><input type="date" id="dateHired" value="@dateHired" /></td>
</tr>
</table>
</form>
Presenting a Static Value
To present a date value as text, in an HTML tag, you can use a DateTime variable preceded with @. Here is an example:
<!DOCTYPE html> <html> <head> <title>Date Values</title> </head> <body> <h1>Date Values</h1> @{ DateTime independance = new DateTime(1960, 1, 1); } <p>Independence Day: @independance</p> </body> </html>
This would produce:
Presenting a String-Based Date
When displaying a date value on a webpage, you can convert the value to a string. Here is an example:
@{
DateTime dateHired = new DateTime(2019, 04, 17);
}
<p>The employee was hired on @dateHired.ToString()</p>
You can also store the value in a string variable before displaying it. Here is an example:
@{ DateTime dateHired = new DateTime(2019, 04, 17); string strDateHired = dateHired.ToString(); } <p>The employee was hired on @strDateHired</p>
Practical Learning: Presenting an Array of Date Values
@{ ViewBag.Title = "United Mexican States"; } <h2>United Mexican States</h2> @{ 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 }; DateTime[] dateOfAdmissionToFederation = new DateTime[] { new DateTime(1823, 12, 20), new DateTime(1824, 2, 7), new DateTime(1823, 12, 22), new DateTime(1824, 5, 7), new DateTime(1824, 7, 6), new DateTime(1952, 1, 16), new DateTime(1917, 1, 26), new DateTime(1823, 12, 21), new DateTime(1823, 12, 21), new DateTime(1869, 4, 17), new DateTime(1824, 1, 10), new DateTime(1857, 2, 5), new DateTime(1952, 1, 16), new DateTime(1824, 2, 7), new DateTime(1823, 12, 23), new DateTime(1823, 12, 20), new DateTime(1849, 10, 27), new DateTime(1856, 9, 12), new DateTime(1823, 12, 23), new DateTime(1830, 10, 14), new DateTime(1863, 4, 29), new DateTime(1974, 10, 8), new DateTime(1824, 5, 7), new DateTime(1869, 1, 16), new DateTime(1856, 12, 9), new DateTime(1823, 12, 23), new DateTime(1823, 12, 23), new DateTime(1823, 12, 22), new DateTime(1823, 12, 22), new DateTime(1824, 5, 22), new DateTime(1824, 9, 14) }; } @{ int i = 0; } <table border="6" style="width: 700px" cellpadding="2" cellspacing="1"> <tr> <td class="text-center"> </td> <td> </td> <td colspan="2" class="text-center"><b>Area</b></td> <td colspan="2" class="text-center"><b>Admission to Federation</b></td> <td> </td> </tr> <tr> <td class="text-center short-text">#</td> <td><b>State Name</b></td> <td class="text-center"><b>Sqr Kms</b></td> <td class="text-center"><b>Sqr Miles</b></td> <td class="text-center"><b>Date</b></td> <td class="text-center"><b>Order</b></td> <td><b>Capital</b></td> </tr> @while (i < dateOfAdmissionToFederation.Length) { <tr> <td class="text-center">@(i + 1)</td> <td>@states[i]</td> <td class="text-right">@areasSqrKms[i]</td> <td class="text-right">@areasSqrMiles[i]</td> <td class="text-center">@dateOfAdmissionToFederation[i]</td> <td class="text-center">@ordersOfAdmissionToFederation[i]</td> <td>@capitals[i]</td> </tr> i++; } </table>
Formatting a Date Value
Converting a Date to a String
When presenting a date value in a webpage, you have many options in deciding how you want the value to appear. As one option, the DateTime structure is equipped with the following version of the ToString() method:
public string ToString (string format);
This version takes a string as argument. That argument specifies the way you want the value to appear.
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 {} placeholder to specify how the value should appear.
String Interpolation
String interpolation is another technique used to format a string. You user 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 DateTime variable, a colon, and a format.
The Year of a Date Value
Introduction
A date value is made of different sections for each value or characteristic.
To let you get the year part of a date, the DateTime structure is equipped with a read-only property named Year:
public int Year { get; }
Here is an example:
<!DOCTYPE html> <html> <head> <title>Exercise</title> </head> <body> <h1>Exercise</h1> @{ DateTime dateHired = new DateTime(2019, 04, 17); int year = dateHired.Year; string strDateHired = $"The employee was hired in {year}"; } <p>@strDateHired</p> </body> </html>
This would produce:
As you can see, the DateTime.Year property produces its value with 4 digits. Another way to get such a value is to pass the format as yyyy. Here is an example:
@{ DateTime date = new DateTime(2002, 4, 2); string strYear4Digits = date.ToString("yyyy"); } <p>Date and Time: @date</p> <p>Year Value: @strYear4Digits</p>
This would produce:
If the year of a DateTime variable or any date value was provided with two digits, such as 98, it would still be produced with 4 digits. Consider the following example:
@{ DateTime date = new DateTime(98, 4, 2); string strYear4Digits = date.ToString("yyyy"); } <p>Date and Time: @date</p> <p>Year Value: @strYear4Digits</p>
This would produce:
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.
If you want to produce a 2-digit year, pass a format with a double y as yy. Here is an example:yy
@{
DateTime date = new DateTime(2002, 4, 2);
string strYear2Digits = date.ToString("yy");
}
<p>Date and Time: @date</p>
<p>Year Value: @strYear2Digits</p>
This would produce:
A Leap Year
A leap year is a year in which the month of February has 29 days. To let you find out whether a year is a leap year, the DateTime structure is equipped with a static method named IsLeapYear. The syntax of this method is:
public static bool IsLeapYear(int year);
This method takes an integer argument and examines it. If the argument, which must be a valid year number, is a leap year, the method returns true; otherwise, it would return false. Here are two examples:
<!DOCTYPE html> <html> <head> <title>Leap Year</title> </head> <body> <h1>Leap Year</h1> @{ DateTime date1 = new DateTime(1988, 10, 6); DateTime date2 = new DateTime(1990, 8, 12); } <p>@date1.Year was a leap year: @DateTime.IsLeapYear(date1.Year)</p> <p>@date2.Year was a leap year: @DateTime.IsLeapYear(date2.Year)</p> </body> </html>
This would produce:
The Month of a Date
The Numeric Month of a Date
To let you get the month value of a date, the DateTime structure is equipped with a read-only property named Month:
public int Month { get; }
The DateTime.Month property produces the numeric month of its date. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<h1>Exercise</h1>
@{
DateTime dateHired = new DateTime(2019, 04, 17);
string strDateHired = string.Format("The employee was hired in {0}", dateHired.Month);
}
<p>@strDateHired</p>
</body>
</html>
If the date occurs between January (included) and September (included), the digit of the month is produced. The above code will produce:
If the number of the month is between 1 and 9 and you want to display the result with a leading 0, pass the double M (in uppercase) as argument. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Date Values</title>
</head>
<body>
<h1>Date Values</h1>
@{
DateTime date = new DateTime(2002, 4, 22);
string strMonth = date.ToString("MM");
}
<p>Date and Time: @date</p>
<p>Month Value: @strMonth</p>
</body>
</html>
This would produce:
The Long Name of a Month
The months of a year are primarily recognized by a name. The names of the months in US English are January, February, March, April, May, June, July, August, September, October, November, and December. To get the name of a mongh, pass a format as a quadruple M as MMMM (in uppercase). Here is an example:
@{
DateTime date = new DateTime(2004, 10, 23);
string strMonth = date.ToString("MMMM");
}
<p>Date and Time: @date</p>
<p>Month Name: @strMonth</p>
This would produce:
The Short Name of a Month
The months of a year are secondarily recognized by their shorts names. The short names of the month in US English are Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, and Dec. To get the short name of the month of a date, pass the form with a triple M as MMM (in uppercase).Here is an example:
@{
DateTime date = new DateTime(1998, 1, 12);
string strMonth = date.ToString("MMM");
}
<p>Date and Time: @date</p>
<p>Month Name: @strMonth</p>
The Day of a Month
Introduction
To let you get the day value within the month of a date, the DateTime structure is equipped with a read-only property named Day:
public int Day { get; }
Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<h1>Exercise</h1>
@{
DateTime dateHired = new DateTime(2019, 04, 08);
string strDateHired = string.Format("The employee was hired on day {0} in {1}",
dateHired.Day, dateHired.ToString("MMMM"));
}
<p>@strDateHired</p>
</body>
</html>
This would produce:
If the day value is between 1 and 9, if you want to display the value with a leading 0, pass the format as dd. Here is an example:
@{
DateTime date = new DateTime(2002, 4, 2);
string strDay = date.ToString("dd");
}
<p>Date and Time: @date</p>
<p>Month Value: @strDay</p>
This would display:
The Day of the Week
The DateTime structure is equipped with a property that can be used to retrieve the day of the week for a given date. The property is named DayOfWeek. To get the day of the week, access the DayOfWeek property a your DateTime value or variable. The values are stored in the DayOfWeek enumeration. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Week Day</title>
</head>
<body>
<h1>Week Day</h1>
@{
DateTime date = new DateTime(1988, 10, 6);
}
<p>Day of the week: @date.DayOfWeek
</body>
</html>
This would produce:
The Day of a Year
The DayOfYear is the number that represents the ranking day inside of the Year value of the variable. For example, if the variable's value is February 6, 2002, the DayOfYear value is 31 (for January of that same year) + 6 = 37
The Short and Long Date Formats
A Short Date
A date is referred to as short if it includes (only) the numeric portions of the month, the day, and the year of a date value. To let you get the short date value of a date, the DateTime structure is equipped with a method named ToShortDateString. Its syntax is:
public string ToShortDateString ();
Another to get this value is to pass a format as "d" (one d in lowercase). Here is an example:
@{ DateTime date = new DateTime(2004, 10, 23); string strDate = date.ToString("d"); } <p>Date and Time: @date</p> <p>Date Portion: @strDate</p>
This would produce:
You can also get this value by formatting a string. In this case, in the string of the string.Format() method, create a {} placeholder. In the placeholder, type the incrementing number, a colon, and d. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<h1>Exercise</h1>
@{
DateTime dateHired = new DateTime(2019, 04, 08);
string strDateHired = string.Format("The employee was hired on {0:d}",
dateHired);
}
<p>@strDateHired</p>
</body>
</html>
You can also get this value with string interpolation. To do this, in a {} placeholder, typ the name of a DateTime variable, a colon, and d. Here is an example:
<!DOCTYPE html> <html> <head> <title>Exercise</title> </head> <body> <h1>Exercise</h1> @{ DateTime dateHired = new DateTime(2019, 04, 08); string strDateHired = $"The employee was hired on {dateHired:d}"; } <p>@strDateHired</p> </body> </html>
The Long Date Format
A date is referred to as long if it includes the names of the month, the name of the week day, the number of the day, and the year. This is called the Long Date Format. To let you get the Long Date of a date, the DateTime structure is equipped with a method named ToLongDateString, Its syntax is:
public string ToLongDateString ();
Here is an example:
<!DOCTYPE html> <html> <head> <title>Exercise</title> </head> <body> <h1>Exercise</h1> @{ DateTime dateHired = new DateTime(2019, 04, 08); string strDateHired = string.Format("The employee was hired on {0}", dateHired.ToLongDateString()); } <p>@strDateHired</p> </body> </html>
This would produce:
As another way to get a long date format, pass a "D" (one d in uppercase) string to the ToString() method. Here is an example:
@{ DateTime date = new DateTime(2004, 10, 23); string strDate = date.ToString("D"); } <p>Date and Time: @date</p> <p>Date Portion: @strDate</p>
This would produce:
Practical Learning: Ending the Lesson
|
||
Previous | Copyright © 2001-2019, FunctionX | Next |
|