Python Part 14c - Working with dates and times Office

Python Part 14c - Working with dates and times Office So welcome to the third of the four part series of tutorials on working with data types in python uh this part is going to look at dates and times so here's what we'll cover during this.

Tutorial we'll first look at how you can get dates and times whether you want to get today or whether you want to get a specific date or time and then we'll look at how you can.

Format them using the strf time function we'll then look at how you can use the calendar module for all sorts of different things and finally we'll look at an alternative.

Way to format dates using the calendar module in part as always you'll be able to click on the link at the top right which should appear about now to.

Download any files to do with this tutorial and if you can't get them there you can get to them by clicking on the link on this tutorials page in youtube and with that i think it's time for me.

To disappear as has become customary and hand over to sven so let's get started let's begin by getting today's date so what i'll do is create a variable.

Python Part 14c - Working with dates and times

To hold it i'll call it the day date and i'll set that equal to the date time module except if you're going to work with dates and times you need to remember to.

Import that module now if i go back you should see that it autocompletes to give me the datetimetoday function and it is a function if i let my mouse.

Linger over it you can see it comes well it's a method which amounts to the same thing so i need to put open and close brackets after that so what i can now do is print that out.

To see what it looks like and if i run that program you'll see it gives you not just today's date which is the second of november as it happens but it's also 2 28 in the afternoon.

You can also do the same thing using the now function so what i could do if i wanted to is copy and paste these two lines of code.

And then select them and what i'll do is press ctrl h to do a replacement and i'll replace today with now in the currently selected text.

    So i choose replace all now

    And run that you can see that i get now date and today's date being exactly the same thing so i'm not quite sure whether it's.

    Duplication there use whichever you prefer what sort of variables have i just created let's find out so if i now print out the type of my variable called.

    Today's date you can see it is a datetime.datetime so this is a class created in the module daytime it's not a built-in python class.

    So that's how you can use today's date in today's time what happens if you want to get a different date so what we're going to do now is get a very very memorable date indeed.

    Which is the time date and time at which the first plane struck the twin towers so to do this i can use datetime.date and then in the brackets i can specify the year which was 2001 i hope the month.

    Which was and the date which was 11. and then i could print that out and also i think what i'll do is print out the type of it i want to see what.

    Sort of a thing i've got and just to get rid of this other stuff so it doesn't clutter on my printout i'll press ctrl forward slash to comment that out.

    So if i now run that you can see it gives me that the date at which the plane struck between towers was the 11th of ninth 2001 and i've created something which is the.

    Datetime.date if i wanted to be more specific i could create another variable called memorable time perhaps and i could set that to be.

    Datetime.datetime and if i do this i can

    Specify six arguments which are the year the month oops the day the hour now i'm probably wrong about this but i seem to remember it's about 8.

    45 in the morning the minutes and the second and if i print out that detailed the type of it see i've got a very slightly different.

    Thing when i run that you can see it's giving me the date and the time component too and it's a slightly different sort of variable and that is how dates times and times.

    Works in python what we now look at is how to get them to look pretty and how to format them so having looked at how you can get dates and times how can you display them.

    Nicely and to do that we're going to use the strf time function and to do that we're going to need what codes to supply to it it's i must admit.

    For this little bit it's as if they left the python ease of use principle behind but see what you think so i'm going to print out uh the memorable time i've created above so.

    This is roughly when the first plane struck the twin towers i want to print it out with the day name the day number the month name and the year number and then the time.

    So to do this i can type in my memorable time and then i can use the strf time function and i can put in arguments to make it work but as to what i type in.

    There this is where life gets complicated included with it with the tutorial are two files called date formatting.png and timeformatting.png.

    They're images so let's look at the dates first i want to get the day name well the day name is over here it's it's going to display like that so to get that i'm going to.

    Have to put a percent capital a in to get the day number that's up here i'll use a percent d at least d stands for day and then to get the month name that's.

    Over here so i'll use percent b for month it doesn't make much sense does it and to get the year number as a four digit string.

    I'll use percent y so i hope you've remembered those if i now try getting that to work within my quotation marks so i'll type in percent.

    Capital a for the day name percent little d for the day number percent capital b for the month name and then percent capital y for the year.

    Number it's not that easy to remember is it if i then try printing that out it will give me the day nicely formatted but why couldn't they just use excel.

    Style formatting strings i'm sorry i'm moaning so now what i'll do is print out the time so to do this it'll be pretty much exactly the same command.

    It's just that my formatting will change so to do times we want to print out the hour as a two-digit number up to 12 so that'll be this little.

    Fellow and to do that i'll use percent capital i to get the minutes and the seconds.

    I'll use percent m and percent s that makes sense and to get the ampn i'll use percent little p so again hopefully we remember that.

    Let's try this out within the quotation marks i can type in percent i to give the hours uh colon percent capital m to give the.

    Minutes column percent capital s to give the seconds and then space percent little p to give the am pm and if i try running that you can see it.

    Gives me the time as well it's such a difficult thing to remember and i dislike it so much in a bit i'm going to show you a different way to do the same thing.

    I want to do now is just to give you an idea of some of the things you can do with the calendar module so i've created a file called calendar module and i've got my usual data there i've given it a.

    Shorter variable name so that i can refer to it easily so the first thing you can do with a calendar is you can print it out so to do this i'm going to print out.

    Calendar dot month and unfortunately i haven't actually imported my calendar module which is why that wasn't auto completing.

    And then after the month i can put in the year i'm going to go for this year i'm going to go for the current month as the time of speaking which is november.

    And then the third argument i can put in is how many characters are assigned to each column so i'm going to go for four characters because it gives reasonably good results.

    You'll see exactly what i mean by this in a second if i now run that you can see i get the month i've chosen and i've got four characters for each column which gives.

    Enough room to show the day name as a three character string that's one use of it another use of the calendar module is forgetting uh abbreviations of days and.

    DISCLAIMER: In this description contains affiliate links, which means that if you click on one of the product links, I'll receive a small commission. This helps support the channel and allows us to continue to make videos like this. All Content Responsibility lies with the Channel Producer. For Download, see The Author's channel. The content of this Post was transcribed from the Channel: https://www.youtube.com/watch?v=blCIQKiR4Kg
Previous Post Next Post