There are four primitive data types that you will be consistently working with throughout your C# programming career:

  • string

  • int

  • double

  • bool

In this lesson, you will be introduced to another data type that allows you to work with dates and times. It’s called DateTime, but unlike the previously mentioned simple data types, DateTime is a complex data type. It’s complex in that it is a class, defined in the .NET Framework class library, that itself is composed of a variety other data types. A DateTime variable (or object) contains within it, for example, three separate int variables called Second, Minute, and Hour, which hold those individual time values. Now, this topic – relating to classes, object-oriented programming, and the .NET Framework – is outside of the scope of this lesson. But it is worth noting up front the difference between complex and simple data types.

Step 1: Create a New Project

As always, you can either setup the Project and Default.aspx page, or use the provided code in the Before folder for this Lesson.

For this demonstration, create a new ASP.NET project and call it “CS-ASP_014.” Set up Default.aspx with the following Server Controls, and programmatic IDs:

  1. okButton

  2. resultLabel

cs-asp-014---working-with-dates-and-times.001

Step 2: Create and Access a Variable of Type DateTime

In the okButton_Click event, create a DateTime variable, and set it to the current time, using the "Now" property:

cs-asp-014---working-with-dates-and-times.002

When you hover over a property, such as the “Now” property in DateTime.Now, you will often find a helpful tip about what it represents:

cs-asp-014---working-with-dates-and-times.003

Now that we have the current time, let’s try to output it by assigning it to resultLabel.Text:

cs-asp-014---working-with-dates-and-times.004

This error informs us that we need to use a helper method to convert this variable type to a string. All you need to do to make the error disappear is convert it to a string using ToString(), and you can now run the application:

cs-asp-014---working-with-dates-and-times.005

cs-asp-014---working-with-dates-and-times.006

Step 3: Formatting Date Values Returned from DateTime.Now

Keep in mind that the formatting order shown here (Day/Month/Year, Hour/Minute/Second, AM/PM) is dependent on your Windows localization settings on your computer. There are other ways of formatting the current time, and there are a number of helper methods you can access to facilitate this. Go ahead and try a few of the “To” methods available to DateTime variables:

cs-asp-014---working-with-dates-and-times.007

Write out these methods to see the different formatting results they produce (be sure to comment out the ones you are not currently testing to avoid getting a compilation error):

cs-asp-014---working-with-dates-and-times.008

cs-asp-014---working-with-dates-and-times.009

cs-asp-014---working-with-dates-and-times.010

cs-asp-014---working-with-dates-and-times.011

cs-asp-014---working-with-dates-and-times.012

Test out these other helper methods, which require a ToString() appended on the end since each of these return values that are not of type string:

cs-asp-014---working-with-dates-and-times.013

cs-asp-014---working-with-dates-and-times.014

cs-asp-014---working-with-dates-and-times.015

cs-asp-014---working-with-dates-and-times.016

cs-asp-014---working-with-dates-and-times.017

cs-asp-014---working-with-dates-and-times.018

cs-asp-014---working-with-dates-and-times.019

Here is what each of these methods, specific to DateTime, do:

  1. Returns a DateTime two days forward from the current DateTime.

  2. Returns a DateTime two months backward from the current DateTime.

  3. Returns the month, as an int, from the current DateTime.

  4. Returns a bool expressing if the current DateTime is at a point during daylight savings.

  5. Returns the day, as a string, from the current DateTime.

  6. Returns the day in the year, as an int, from the current DateTime.

Tip: You may have noticed that some of the helper methods above have helper methods that operate on them. This is a useful technique in C# called chaining that allows you to chain methods together. For instance, myValue.AddDays(2) will return a DateTime value. AddDays() performs one operation on the myValue variable, and once that operation is complete, ToString() takes myValue and converts it to a string. You can do this because each helper method is affecting the variable, performing multiple operations on it within a single line of code.

Step 4: Parsing a String into a DateTime via DateTime.Parse()

Next, let’s feed a particular date into a method that parses through it (performing an algorithm) and returns DateTime information it was able to parse from it:

cs-asp-014---working-with-dates-and-times.020

cs-asp-014---working-with-dates-and-times.021

Tip: The date here – 12/7/1969 – happens to be my birthday, which I know occurred on a Sunday. You may find it more interesting to input your own birthday to see what day you were born on.

Alternatively, you can also use the "new" keyword and initialize the DateTime by passing the values in between the parentheses:

cs-asp-014---working-with-dates-and-times.024

Here, we’re setting the properties stored in myValue to:

  1. Year

  2. Month

  3. Day

  4. Hour

  5. Minute

  6. Second

You have many different ways of initializing the DateTime, and you can get a list of them by typing in the parentheses and using the up/down arrows on your keyboard to cycle through the variations:

cs-asp-014---working-with-dates-and-times.025

This is a preview of both a special kind of method called a “Constructor,” as well as what is called “Constructor Overloading.” There isn’t anything very fancy about Constructors other than the fact that they are methods with the same name as the type they belong to. In this case, the Constructor DateTime() belongs to the DateTime value. They are used for initializing certain values determined by what’s written in the Constructor. Overloading, meanwhile, refers to variations of the same Constructor that have different initialization procedures. Don’t worry too much about understanding this at this point as the process will be made much clearer in subsequent lessons. Keep up the good work, you’re doing great!


Lessons in this Course