Locked lesson.
About this lesson
Let's explore the various functions we can use to display and use the DateTime data type.
Exercise files
Download this lesson’s related exercise files.
8 - DateTime Functions.docx60.9 KB 8 - DateTime Functions SOLUTION.docx
57.9 KB
Quick reference
DateTime Functions
DateTime Functions allow us to do things with our DateTime instances.
When to use
Use these as needed.
Instructions
DateTime myDate = DateTime.Now;
myDate.ToShortDateString(); // Just returns the Date
myDate.ToShortTimeString(); // Just returns the Time
myDate.ToLongDateString(); // Returns Long Date With Words
myDate.ToLongTimeString(); // Returns Time With Seconds
myDate.AddDays(10); // Add 10 Days
myDate.AddDays(-10); // Subtract 10 Days
myDate.AddYears(20); // Add 20 years
Hints & tips
- Use the DateTime Functions to manipulate your dates and times as needed
- 00:01 Again, in the last video we talked about date time.
- 00:04 In this video I want to show you a few fun things you could do with date time to sort
- 00:08 of play around with it.
- 00:10 So we ended off in the last video with DateTime.Now.
- 00:13 And if we run this real quick, we see that it is 9 21 21 at 10:16 AM.
- 00:17 So what if we don't want all of this return?
- 00:19 What if we just want parts of it return?
- 00:22 Well there's all kinds of different things we can play around with to get that
- 00:26 effect.
- 00:27 So we could come down here and we can use different built in functions that
- 00:32 C-Sharp has that allow us to modify how the date is returned.
- 00:36 So if we want just the date, we can call .ToShortDateString,
- 00:42 and that's a function, right?
- 00:45 And notice the capitalization on here is important.
- 00:48 So if we run this, now it just returns the date,
- 00:51 the time is no longer on there, the 9 21 21, very cool.
- 00:55 Let me copy this and let's make a little list here.
- 00:59 We can also return just this time.
- 01:02 So instead of ToShortDateString, we can do the ToShortTimeString.
- 01:07 We're converting it to the short time string, right?
- 01:10 That's how you would sort of read that.
- 01:11 So save this and run it.
- 01:14 Boom, now it's just 10:17 AM, very cool.
- 01:17 We can also use the ToLong option.
- 01:21 So ToLongDateString.
- 01:25 Save this and run it.
- 01:29 We get Hello Tuesday September 21st, 2021.
- 01:32 It's no longer just the numbers,
- 01:34 it's sort of spelled it out with actual words Tuesday's, so a number, all right?
- 01:39 Very cool.
- 01:40 We could do the same thing with time string, right?
- 01:46 And here we get 10:18 and 32 seconds.
- 01:48 Remember, before it just said 10:18 or 10:14 or whatever the time was,
- 01:53 it didn't have the seconds.
- 01:55 So maybe not that useful, but still nonetheless there we go.
- 01:59 We can also add to our date or to our time.
- 02:03 So if we want to say .AddDays and we can then pass in a number.
- 02:09 So, if we want to say add 10 days.
- 02:13 If we save this and run it,
- 02:17 You see now it says 10 1 21 days, 9 21, right?
- 02:21 So maybe 10 days is not a good day and
- 02:25 this example let's say add 8 days, right?
- 02:30 So today is the 21st, so
- 02:32 this should return 29 as we're adding 8 days and it does 9 29.
- 02:37 So that's cool, let's go ahead and copy this guy.
- 02:42 We can also add negative here.
- 02:44 So today is the 21st, if we add negative 10,
- 02:48 that will bring it down to September 11th.
- 02:52 So if we run this, that says 9/11/21, right?
- 02:57 So that's cool.
- 02:59 We can also add other things too, years.
- 03:03 We can add hours.
- 03:04 We can add minutes, right?
- 03:07 So if we wanted to add years onto here, for instance, we can run add years.
- 03:11 And you can see as I start to type these,
- 03:14 it pops up here in the IntelliSense thing here.
- 03:16 So we could just hit the tab key to sort of auto put it on there.
- 03:21 And here same thing, we can pass in, for instance 10.
- 03:24 If we save this and run it, should be 2031 instead of 2021.
- 03:27 And sure enough, it says 2031, very cool.
- 03:28 So again, you can come through here yourself and play around with this.
- 03:34 If you type in Add, you can see a list of things here.
- 03:37 Add days, hours, milliseconds, minutes, months, seconds.
- 03:40 We can scroll down, ticks.
- 03:43 I'm not sure what that is.
- 03:44 Years, all the things.
- 03:46 So you can play around with this and see yourself.
- 03:48 But that's date time, very useful and very easy to use.
- 03:52 And you're going to use this a lot in all of your coding, and it's just that simple.
- 03:56 So in the next video, we're going to start to talk about nulls.
Lesson notes are only available for subscribers.