There are four primitive data types that you will be consistently working with throughout your C# programming career:
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.
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:
In the okButton_Click event, create a DateTime variable, and set it to the current time, using the "Now" property:
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:
Now that we have the current time, let’s try to output it by assigning it to resultLabel.Text:
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:
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:
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):
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:
Here is what each of these methods, specific to DateTime, do:
Returns a DateTime two days forward from the current DateTime.
Returns a DateTime two months backward from the current DateTime.
Returns the month, as an int, from the current DateTime.
Returns a bool expressing if the current DateTime is at a point during daylight savings.
Returns the day, as a string, from the current DateTime.
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.
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:
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:
Here, we’re setting the properties stored in myValue to:
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:
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!
Lesson 14 - Working with Dates and Times