Datetime Module (Dates and Times) || Python Tutorial || Learn Python Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we live in a large four-dimensional place called please type your location is determined by three coordinates add your place and time as identified by a single number P but here on earth things are a bit more complicated we use a date to determine the day and a time to specify a point during that day once we colonize our solar system a better system will be required until then we must continue to work with dates and times Python takes pity on us Earthlings and gives us a variety of classes for working with clocks and calendars so put on your fancy pants because we have a date all of the classes we will discuss today live inside the date/time module so first things first import the day time module let's see what is inside this module by printing its directory this module contains a date class a time class and a date time class the date class is for working with days the time class is for times and the date time class is a combination of both the date and time classes it is a fully featured class that gives you everything you need to specify a precise moment in time let's first learn about the date class it's always a good idea to look at the help text before using a new class we see that pythons date class will support all dates from the year 1 up until the year 9999 so any code we write should last for quite a while I will wager three bitcoins that human computer hybrids will be using a new language in the year 10,000 if you scroll to the top of the help text it will see how to create a date object to make a date you pass in three numbers the year the month and the day let's make a date called gbr with the date January 31st 1956 this is the birthday of Guido van Rossum the creator of Python if you print gbr you can see the date was created successfully and you can access the Year month and day separately the day time module contains a class called time Delta which lets you add or subtract a number of days from a date for example at Mill B January first two thousand a day when people behaved in an odd and superstitious manner let's assume that it took 100 days for people to return to normal what date would that be to find out create a time delta object and pass in the number of days a positive number will increase the date and a negative number will decrease the date if you print the sum you see that April 10th 2000 was 100 days after crazy day did you notice that by default Python displays dates in year-month-day format but you can specify a different format if you like to do so you must create a format code there are more than 20 different codes for you to choose from when specifying a format for a date or time as an example suppose we want to display the full name of the day followed by the full name of the month the day number than the year there are two ways you can print a date in this format the old way is to call the strf time method on a date then you pass in the format string using the available codes here the codes are % a for the full name of the day % B for the full name of the month % D for the day of the month and % Y for the four-digit year be careful when typing the code it is case-sensitive that is a nice-looking date a more modern way of doing this is to create a string with the format you can enter any text that you like but when you are ready to enter the date patterns start with an open brace followed by a colon now you can print this message to do this call the format method and pass in the date time or date time object that is indeed a well formatted message let us now return to the date/time module inside this module are three key classes date time and date time we will now create objects using all three of these classes basics first launched a reuse rocket on March 30th 2017 at 20 to 27 UTC we can create a launch date object using the date class the three arguments are year month and day to create a launch time we will use the term class here the three arguments are hours minutes and seconds and finally we can create an object with both the date and time using the date/time class the first three arguments are the year month and day and the next three arguments are the hour minutes and seconds let's now print all three objects to see how they differ from one another print the date the time then the date/time you can see that the launch date only contains date information the launch time only contains time information and the launch date time contains everything just like date objects a time object gives you access to the individual components you can access the hour the minute and the second as you might suspect the launch date time object gives you access to the date and time components separately you can access the year month day hour minute and second another common problem is to access the current date time the date/time class has a method called today for just this purpose to get the current date time in UTC format type the module state time the class date/time and then call the method today if you print the date time you will get the exact date and time you run the code on your computer but what's this after the second there's a fractional value did I mention that the date/time class can store times down to the microsecond it's not like me to forget you can access the micro seconds just like the other day time components that's some serious precision another common task is to take a string representation of a date/time and convert it into a date/time object for example let's convert the string of the date when a human first walked on the moon into a date time object to do this we will use a method on the date/time class called ft RP time which is an abbreviation for string cards tons this method name is unfortunate since it is used to parse both dates and times I would have called it par string but nobody asked me when you call this method the first argument is the string containing the date the second argument is the expected format we use the same date/time patterns as before if you print this object Python will display the value in a standard daytime format but it is in fact an object not a string as proof print the type for this object this is indeed an instance of the date/time class in the date/time module the date/time module contains a class called time delta which can store the difference between to date times it can also be used to add or subtract an interval of time to another daytime object we socratic ah are working hard to shrink the time delta between our videos how can you help by implanting the need to watch our videos into the minds of others subtle persuasion that's all I do Oh
Info
Channel: Socratica
Views: 266,370
Rating: 4.936111 out of 5
Keywords: Socratica, SocraticaCS, python, python programming, computer science, datetime, datetimes, module, date, time, timedelta, datetime in python, python datetime, socraticapython, learn python, programming in python, how to python, how to program in python, python tutorial, python tutorial for beginners
Id: RjMbCUpvIgw
Channel Id: undefined
Length: 7min 43sec (463 seconds)
Published: Mon Apr 10 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.