 Welcome back, everyone. This is Brian. And in this video, we're going to talk about date time. Dates and times in other languages are well a pain, but Python makes it ridiculously simple. This video, we're going to talk about dates, times, deltas, and formats. Let's dive in and take a look. The first step, of course, is imports, everybody's favorite. So we're going to say from, and we want the date time, we're going to import date time, and I'm delta. So what are these things? Well, date time is the module and date time is the class we're going to work with. And time delta allows us to take a different time, or I should say time in the past or the future, and add or subtract numbers to it. So you could say, Hey, I want to know the date 30 days from now or five weeks from now. And we want to be able to format these things. So I'm gonna say from time imports. And we want to string format time. What that's going to allow us to do is say something like, today is December 2017. But put it in a nice pretty string format. All right, to begin, let's go ahead and make a main function here. And we're going to talk about now, meaning right now, we're going to do this literally right now. What is now exactly? Well, it is a representation of the current date and time. So I'm gonna say date time dot now. Now you should understand what's going on under the hood when you do this. You're saying go out to the operating system and say operating system, what date time do you believe it to be? However, that might not always be the actual date time. So I'm gonna say date time dot UTC. Now, special note here, if the machine is on a UTC time zone, then there'll be absolutely no difference. So which one is right for you? Well, it depends on which one you want. Personally, I tend to use the UTC. Let's go ahead and just print some stuff out. I'm just going to through the magic of copy and paste here. We're going to say print out now print out UTC and I want that offset. So let's go ahead and print. You see, there's absolutely no offset here. So these are exactly the same. Now, they're not actually exactly. You notice these last two little numbers here. That is actually the difference in time between this print and that print. That is your computer running. That is an incredibly small amount of time. It is called a microsecond. Let's talk about time. Time is split into logical units and don't worry, you're not going to sit here and watch me type for hours on end. But it is split into logical units, meaning you have hours, minutes, seconds and microseconds. We run this. Everything just seems to work. It is the 16th hour, 17 minutes, 27 seconds, and there is our microseconds. And of course, milliseconds being up here. Now, you may be wondering, what's the difference between now and UTC? Again, because there is no offset, they should be identical. This is a very simple, easy way of grabbing this. There are a lot of tutorials out there that show you some long exhaustive ways of doing it. But why bother? Just grab the daytime now or daytime UTC now and then grab the hour, minute, second or microsecond, depending on what you need. Time waits for no one and time is precious. So I don't want to waste too much of your time here, which is why I'm doing a lot of copying, pasting. Dates are well just as simple. I'm just going to do the old copy and paste for dates. Year is now year month, now month, day is now day. Now, you may notice sometimes if you're typing away here, it may or may not depending on what you're working with. Let me find one here. Hour, there we go. So IntelliSense is picking up two of these, it's saying hour and hour. I'm going to just run this and let's see what happens here. Just wanted to put a quick note in there that sometimes IntelliSense will betray us. Daytime, daytime object has no attribute of hour. If you ever get that, check your case sensitivity. For example, ta-da. But of course, that's not the right thing. Major takeaway from that is case sensitivity does matter. Grabbing the hour, minute, second, microsecond, year, month and day is ridiculously simple. There's no sense in wasting a ton of time trying to figure it out. All right, this is where we slow down a little bit here. We're going to talk about deltas. A deltas is a difference between two numbers. So what we want to do is do some date computations here. For example, let's find what the date is going to be 30 days from now or a month from now, I should say. This is where Python's a little bit lacking it. There are other ways of doing this. I just wanted to show this to you really, really quick. So I'm going to say next month. I'm going to say now plus and we want time delta. Now you're inclined to go, Oh, this must be easy. I'm going to say month. And then Oh, doesn't work. And then you go, Oh, I know the problem. This is uppercasing it out. It still doesn't work. What the heck? Maybe it's months and it still doesn't work. This is one of the shortcomings of the time delta is that it really doesn't have that concept. You have to do it in simpler terms. So I'm gonna say days. Let's go ahead and do it 30 days out and see what this looks like here. Now suddenly I get next month's date, which will be January 16th. Hmm. So it's a shortcoming, but it's really not that big of a deal. There are other ways of doing it, but I don't want to get super, super complex here. So I'm gonna say last week. Now I'm going to say time delta and let's say weeks. And we are going to minus one. So what this does is it takes our current date time and reduces it by whatever number. So last week was 1210. It becomes ridiculously simple from this point. I'm just going to for the sake of brevity copy and paste the rest out. I think you're smart enough if you're watching this series to figure out what's going on here. So five hours from now would be time delta hours five 45 seconds would of course be seconds 45 200 milliseconds 10 microseconds and so on. It is just ridiculously simple to work with this. The major caveat being you're going to start stumbling into little inconsistencies like I displayed with the month. Now sometimes you need to work with a string representation of a date and that's where ISO strings really come into play. So I'm gonna say D equal and I want the daytime dot from ISO format and you notice how you've got a whole lot of things you can say from from calendar from ordinal from timestamp but we're going to do ISO format. Now there is a special format you have to follow. For example, I'm gonna say 2020 dash 12 dash 16. You can very easily go out and Google ISO format and find out what it is. We're gonna go ahead and say print. Notice how there's no time because we didn't include the time in the format. So it is at 00000 basically midnight. Alright, that is super simple to work with but there's one little gotcha you should really understand. So I'm gonna say m equals and I want to say date time. We're going to make this thing crash. And I'm gonna say from ISO format and this is where trusting your end users could be a very bad idea. So let's say 2026 dash I'm just gonna make up some gibberish here. I don't really care. We're intentionally trying to make this thing break. This looks like it could potentially maybe be some kind of date time and no value error. So it crashed our little program here. So you gotta just not trust your user. So I'm sorry, it's really what it boils down to. So once they try, let's do this. Get some screen real estate there bang and there we go exception as EX. And let's just go ahead and print out the args. That way we can see we had an invalid ISO format and it gives us the actual string representation of that. So in case you're kind of curious, yes, you can actually get the ISO format from our now object here. So let's say print, say ISO. And let's say now dot ISO format. And let's print that out. So this is the correct ISO format. And if we had put that in there correctly, it would have converted it. However, we just entered gibberish. So it crashed our little program. One of the more challenging things in programming is well formatting dates and times and every operating system seems to do it a little bit differently. And every programming language does it a little bit differently. So I put a link out here. W3 schools big shout out to them. I'm not affiliated with them by any way shape or form, but they have this really, really great chart and you can actually click this try it button. And it shows you exactly what's going to happen. So really what we're doing is a string representation of what we want the date time to be. And this seems a little bit confusing till you start wrapping your head around what's going on. So for example, a percent sign means we're going to format. And then we have some letter or numerical value depending on what you want. Python really doesn't have numerical values in there, but other languages and frameworks do. So we have percent lowercase whenever you see lower think short version when you see upper think full version however, they don't always line up. So for example, upper case H is our and then this right here is our so what is the difference here? One is in 24 hour format one is in 12 hour format and it's just some of these little gotchas you got to kind of figure out what you need in advance and that's where a chart like this comes really super handy. All right, another really good example would be like capital a weekday full version, lowercase w weekday as a number. So you can do a lowercase and uppercase or W YW. Why not? It's just how they designed it. So let's just dive in here and take a look. Let's say print. And let's go ahead and format this out. And we're going to say, actually, no, I don't want to format that out. Go ahead and say now dot string format time. And this is where those special codes would come in. So I'm going to say we want to format something. So the percent sign. And let's do a Y, which represents the year. So it's 2020. However, that's kind of just the number 20 doesn't really represent how horrible 2020 spin. So let's just uppercase that bad boy. And there it is 2020. You kind of get the point. So I'm going to do a little bit of copy and pasting. Just to speed that up just a microsecond here. So for example, percent D, D, percent B, see what those do. And you can find out basically off that chart very quickly how you want to format that. Now you may be asking yourself, there's got to be a way to do this a little bit more complex than this. I mean, come on, this seems overly simple. Yes, this can make your life super, super simple. For example, let's say we want to do something like this. Today is and let's go ahead and say percent B. And then percent lower case D. Today is December 17. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the Voidrom's Facebook group. This is a large group with lots of developers and we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. Drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become, the more I'll create and publish out on YouTube. Thank you for watching.