Locked lesson.
About this lesson
We introduce the Datetime data type and explain how to set it to a specific value.
Exercise files
Download this lesson’s related exercise files.
7 - Datetime.docx60.7 KB 7 - Datetime SOLUTION.docx
57.6 KB
Quick reference
Datetime
Datetime is another Data Type.
When to use
Datetime is used when you need to deal with a date or a time.
Instructions
- DateTime myDate = new DateTime();
- Return a specific date (October 14, 2021):
DateTime myDate = new DateTime(2021, 10, 14); - Return Current Date:
DateTime myDate = DateTime.Today; - Return Current Date and Time:
DateTime myDate = DateTime.Now;
- 00:00 All right, in this video, I want to talk about DateTime, and
- 00:03 DateTime is another data type.
- 00:05 We didn't talk about it in the last video because it's a little bit more
- 00:10 complicated, and it needs its own video.
- 00:13 But it's just what it sounds like, it's the date and time.
- 00:17 And there are many different times when you're writing code, when you need
- 00:22 to determine the date or a specific time, and you can use DateTime for that.
- 00:27 So let's just jump in here and create one of these guys.
- 00:31 So we call DateTime, and let's call this myDate, and
- 00:34 we're going to set that equal to a new DateTime function.
- 00:38 And we haven't talked about functions and methods yet, but usually these parentheses
- 00:44 mean function or method, depending on the programming language you're using.
- 00:49 And this is something that is built into C#.
- 00:52 This will grab the current DateTime for us and assign it to this variable.
- 00:57 Now, it won't give us the current DateTime, and
- 01:00 we'll see that in just a second.
- 01:02 In fact, let's put this in here and see what this returns.
- 01:06 So we can call + myDate, now let's go ahead and run this guy.
- 01:11 And when you do, we could see it says Hello 01/01/0001 12:300 AM.
- 01:15 So basically, it's the dawn of, well, not the dawn of time I guess, but year zero,
- 01:21 January 1st in year zero, at midnight or 12 AM or whatever you want to call that.
- 01:26 So it's giving us a DateTime format here, but this isn't useful in any way, right?
- 01:32 So we have to actually tell it what kind of time we want, and
- 01:36 we could do this several different ways.
- 01:39 We could be explicit and say, hey, return a specific date.
- 01:43 And we can put in ourselves, so we could say 2021,
- 01:46 let's say October 14th, I don't know.
- 01:49 If we save this and run it, You could see now,
- 01:52 it returns 10/14/2021 at 12 o'clock, right?
- 01:55 So oftentimes, you're not going to do something like that either.
- 02:00 Many times, you're just going to want to know what the current DateTime is.
- 02:04 So there are several different ways we can do that.
- 02:08 We can call .today().
- 02:09 Now, we don't want a new DateTime, we want just whatever today's DateTime is.
- 02:14 So we can go ahead and save this and run it.
- 02:17 When we do, we see 9/21/2021.
- 02:19 Again, it says 12 o'clock AM, talking more about dates than time.
- 02:24 We'll get into time in a minute.
- 02:26 But you could see, this is today's date, and that's pretty cool.
- 02:30 We could also call .now(), sort of a more useful thing to call.
- 02:35 We run this, we see now, 9/21/2021.
- 02:37 Then we also get the exact time, 10:14 and 41 seconds AM.
- 02:41 So very cool, very useful, and kind of all there is to it.
- 02:44 So that's all for this video.
- 02:46 In the next video, we're going to talk a little bit more about DateTime and
- 02:51 some of the fun things you can do with it.
- 02:54 And that'll be coming up in the next video.
Lesson notes are only available for subscribers.