[MUSIC] Hey, everyone in today’s video! I’m going to show you how to use the date time string parsing and the date time date formatting, so we’re going to start off with parsing meaning. We’re going to take these dates here that are just strings and we’re going to convert them to date/time objects, so I already have date/time imported so from daytime import day time and what I’m going to do is, I’m going to take each one of these dates and create a date/time object out of them so first I have this website open stir. F Time org. And it basically has all the things you need to know to either. Parse a date or format a date, so these are just directives that tell daytime what is at a particular position in the string. So for example, if I look at percent lowercase M, we see it says month as a zero pad at decimal number, so anything in that position is going to be treated as a month if it’s an actual day or a year, then it’s not going to work correctly, so you have to make sure the directives are correct, so I’ll show you how to use these, so we have these three dates, and I want to convert them to date/time objects, so I’ll just say date one object, and I’ll do the same for the other two. So the first thing I need to do is use day time, and then I’m parsing this, so I’m taking a string and I’m converting it to a day time object, so I’m parsing the string, so I use the method called Sturt P Time so Strp T IME and it takes in two values. So the first value is the actual date string so date one in this case and the second is the string of the format of the date, So I’ll start with a string and the first thing I need to put in is the month, so I have the month June here. This is the month spelled out, So if I go to this site and look for the month, I see September as an example, and the meaning is month as locale’s full name. So that’s exactly what I want. And the code is percent Capital B so case matters so percent B. And then there’s a space here so anything that is not part of the date, but it’s just a part of the format of the string You’re going to reflect that in your format string here, so I’m going to put a space, so we have the month and then we have a space followed by the day, so let’s take a look here And what we’re looking for is day of the month as zero. Pettit decimal number. So it doesn’t necessarily need to be zero. Pat it. I noticed that percent D works more than percent – D so just use percent D when it comes to days of the month. So that’s the next thing that I put in so percent. D followed by a comma, So the comma isn’t a part of the actual date. It’s just the format and then the year so the year is going to be percent capital. Y ok, so now let’s print this out and run the script. So this is date one object and we see it’s been converted to this format, so this is actually the string representation of a date/time object, So if I do something like print type and we do that again, we see it’s a daytime object, but when we print directly, it ends up as a string just for readability purposes, so it is a daytime object, but when we print it, it converts it to a string automatically because of the stur method on the day time class. So if we were to change this a little bit so instead of June, let’s say December and run this again. We see the month here. So 12 is the month and 6 is the month here, so by just changing the name of the month, the daytime object reflects that change. So now let’s do the other two so day two object so date time and we’re going to parse the time, so this one’s a little different, so this one can be two different things, depending on where you live. So in United States, this is August 10th and in some other countries, this is actually October 8th, So it depends, so let’s go with August 10th first, so we start off with the month, so let’s look for a month, which is a percent in lowercase M followed by a slash, which is a part of the format and then we have the day, which is similar to here so percent D. And then we have the year in two digits. So this is the year 2029 so if we look here a year without century as zero padded decimal number so percent Y lowercase, so percent Y and then let’s just print that out. So this one we see 2029 August 10th So if we switch this format a little bit and we put the day first and then the month second, and we run this now we see it is. October 8th, 2029 So for the last date? This one actually has a time in it. But it’s a similar process, so date 3 object is day time stir. P Time and then date 3 and we’re going to first get the date, so we have month then day, so % M – % D. So the dashes are just part of the formatting and then % Capital Y because we have the year with the century. And now we have the time, so this is 24 hour time so 12 23 and then 3 seconds past the minute. So we’re going to look for the our first, so the hour is capital. H for 24 hour clock so Capital H. And note the space in between the year and the beginning of the time Because there’s a space here, and then there’s a colon and then we have the minute. So the minute is going to be Capital M so % Capital M followed by the second and the second is percent Capital S. And if we print this out, so date 3 object will see, we have not only the date here, but we have the time on that date and the other ones. They just default to midnight because we didn’t supply a time so now let’s format these back to a certain string so to do that. It’s a similar process So instead of call day time. Dot ster. P Time like we did, we’re going to actually use the object itself, so date one object and then we’re going to call the method caught format so STR F times so string format time and this time we just need to pass in a format because we are already starting with a date because we’re calling a method on the day time object. So for this one, let’s say instead of having December 8th, 2011 like this. We want it to look like this, so we want to put the year first so % capital y a space. Let’s have a comma and then we have % B and then we’ll just have the day so % lowercase D and let’s go ahead and just print that out And now we see The year comes first, followed by the month and the day. So, of course. You probably wouldn’t do it that way, but if you wanted to you could or if you wanted to do something like this where you just leave out information, so let’s take a look at the second one, so we’re going to format something so string. F time and this time. We only want the month just the name of the month, so this one is August, so let’s print it out. Actually, it’s October because I switched it. So if I switch this back when I parse it, It would be August again, but I didn’t change it So it shifts October, So we see October here. If we do the year, then we’ll see only the year. So whatever you want, you can leave out information. Of course you can’t add more information because date time needs to know, for example. If we try to ask the time, it’s just going to be zeros because we didn’t supply a time when we first created that particular one, and then finally an example for Date 3 format the time and let’s say we will format with just the minutes in the seconds, so minutes and seconds and we can do that, and we see 23 minutes in 3 seconds. So, however, you want your dates to look, you can make them look that way. Using stir F time and whatever format that the dates come into your app or your scripts, you can read that into date time using the Easter parse time and convert that to a date time perform any operations. You need to perform with it and you can convert it back to any kind of time format that you want so. I hope this video helps I’ll include a link to this site so you can see all of the codes for the directives and I’ll also include the code here in the description below. So if you have any questions about this, feel free to leave a comment below? If you like this video, please give me a thumbs up, and if you haven’t subscribed to my channel already, please subscribe, so thank you for watching, and I will talk to you next time.