Download as PPTX, PDF, TXT or read online from Scribd
Download as pptx, pdf, or txt
You are on page 1of 19
What is Datetime in Python?
datetime is a built-in Python module.
datetime is a single module. It can import this datetime module in such a way that they work with dates and times. Developers don’t need to install it separately. With the datetime module, you get to work with different classes that work perfectly with date and time. working on Python, date, and datetime are two separate objects. If you modify them in any way, you’re modifying the objects; not the timestamps or strings. Within these classes, you can get a range of functions that deal with dates, times, and different time intervals. Examples of Datetime in Python get the current date using datetime in Python: # importing the datetime class
from datetime import datetime
#calling the now() function of datetime class
Now = datetime.datetime.now()
print("Now the date and time are", Now)
The output is as follows: To count the difference between two different datetimes.
#Importing the datetime class
from datetime import datetime #Initializing the first date and time time1 = datetime(year=2020, month=5, day=9, hour=4, minute=33, second=6) #Initializing the second date and time time2 = datetime(year=2021, month=7, day=4, hour=7, minute=55, second=4) #Calculating and printing the time difference between two given date and times time_difference = time2 - time1 print("The time difference between the two times is", time_difference) output is: What is the Import Datetime Statement?
to import a particular class like date, time,
datetime, timedelta, etc. from the datetime module, # importing the date class from datetime import date # importing the time class from datetime import time # importing the datetime class from datetime import datetime What are the Commonly Used Classes in the Datetime Module? There are 6 main classes when it comes to datetime in Python. Date we mean the conventional concept of dates with effect to the Gregorian Calander. the date class in Python is explained with the attributes like day, month, and year. Time This class is independent of any day. Here it is generally assumed that each day has 24*60*60 seconds. The attributes of the time class in Python include minute, second, microsecond, hour, and tzinfo. Datetime Combination between dates and times. The attributes of this class are similar to both date and separate classes. These attributes include day, month, year, minute, second, microsecond, hour, and tzinfo. Timedelta It the variation between two different dates, time, or datetime examples to microsecond resolution. Tzinfo tzinfo is an attribute. It can provide you different timezone-related information objects. Timezone This class can deploy the tzinfo abstract base class in terms of a fixed offset from the UTC. Here, you are looking into the new UTC version 3.2. What Are the Various Functions Available Within Date Class?
When any particular object within the
date class is embodied, the date format is represented in this way - YYYY-MM-DD. Hence, a syntax of this particular class requires three specific arguments viz. year, month, and day. Example 1: Date Object to Represent a Date Your arguments should be in the range mentioned below: Year must be between MINYEAR and MAXYEAR The month should be between 1 to 12 The day should be between 1 to the number of days in a month or year # Explaining date class # importing the date class from datetime import date # initializing syntax with arguments in the format year, month, date Date = date(2021, 7, 4) print("Date is", Date) output: whenever there is any anomaly while passing any argument, Python will raise ValueError. Example: There are 31 days in May (5th month). If Python gets an argument like a date(2021, 5, 32) it will show an error. Also, the arguments should be integer only. Otherwise, it will show TypeError if it gets any string or float as an argument. Get Current Date While working with datetime in Python, one can create a date object that contains the current date with the help of a classmethod. It is named ‘today()’. You can print components like the year, month, and date of today separately. In that case, you’ll need to call an individual agent. # To print current date # importing the date class from datetime import date # calling the today() function of date class Today = date.today() print("Today's date is", Today) print("Today’s components are", today.year, today.month, today.day) Here’s the output: Get Date From a Timestamp
Did you know that creating dateobjects from a timestamp
is also possible? Think of a Unix Timestamp. It is basically the number of seconds between any specific date and January 1, 1970, at UTC. Converting a timestamp is also possible. But in that case, you have to use the fromtimestamp() method. # To get timestamp from a date # importing the date class from datetime import date Timestamp = date.fromtimestamp(1000000000) print("Date is", timestamp) output: How Can You Handle Timezones in Python? #To get the local time of specific place from datetime import datetime #Importing third-party module pytZ import pytz #Calling local time Local_datetime = datetime.now() print("Local date is", Local_datetime) #Calling local time of Toronto Timezone_Toronto = pytz.timezone('America/Toronto') Local_datetime_Toronto = datetime.now(Timezone_Toronto) print("The date and time in Toronto is",Local_datetime_Toronto) #Calling local time of Tokyo Timezone_Tokyo = pytz.timezone('Asia/Tokyo') Local_datetime_Tokyo = datetime.now(Timezone_Tokyo) print("The date and time in Tokyo is",Local_datetime_Tokyo) output