10.0 DateTime
File Location
Right click on your solution
Go to Add > New Project and select Console App (.NET Framework)
Name it
DateTime
Discussion
DateTime
is a built in function within C# that helps use time and calender dates to display and manipulate information. There are a multitude of things we can do with DateTime
to get the information we need.
Lets start with one example of declaring a date and having it compare with today's date by using .Today
.
Notice when it prints a date to the console it will look like
1/15/2018 12:00:00 AM
We use the
.Today
function to call today's date and time and then we ask the console if it is equal to the value we set.It is not equal. It will print false.
Here are some different format functions you can have to change the format of your DateTime
.
ToString() -- Will convert your DateTime to a string value. ToShortDateString() -- Will display the Date value but not the Time value. ToShortTimeString() -- Will display the Time value but not the Date value. ToLocalTime() -- Returns the local time that corresponds to a specified date and time. ToLongDateString() -- Will return your date in a long format that will look similar to Wednesday, May 16, 2018
. AddDays() -- Will add specified amount of days to your DateTime
. AddHours() -- Will add specified amount of hours to your DateTime
.
There are others but this is a good start.
Challenges
Bronze
Print your birthday to the console in three different formats.
Silver
Using the DateTime
class, create some logic that will print to the Console how many days old you are.
Gold
Can you use DateTime to check the date and time overseas in a couple different countries? How about creating a timeline of key battles from World War II?
Answers
Next: LINQ
Last updated