Python Date and Time
Python Date and Time provides time package to deal with Date and time. It helps to retrieve current date and time and can be used to manipulate with the help of built-in methods.
Working with Python Date and Time
Python Retrieving Time Example-
import time localtime = time.localtime(time.time()) print ("Current Time is :", localtime)
Output-
Current Time is : time.struct_time(tm_year=2019, tm_mon=7, tm_mday=24, tm_hour=22, tm_min=44, tm_sec=38, tm_wday=2, tm_yday=205, tm_isdst=0)
In the above case can see the output is not formatted. So the Formatted Datetime-
#formatted import time localtime = time.asctime( time.localtime(time.time()) ) print ("Formatted time :", localtime)
Output-
Formatted time : Wed Jul 24 22:45:08 2019
So below is an example to find –
a) Current date and time
b) Current year
c) Month of year
d) Week number of the year
e) Weekday of the week
f) Day of year
g) Day of the month
h) Day of week
import time import datetime print("Current date and time: " , datetime.datetime.now()) print("Current year: ", datetime.date.today().strftime("%Y")) print("Month of year: ", datetime.date.today().strftime("%B")) print("Week number of the year: ", datetime.date.today().strftime("%W")) print("Weekday of the week: ", datetime.date.today().strftime("%w")) print("Day of year: ", datetime.date.today().strftime("%j")) print("Day of the month : ", datetime.date.today().strftime("%d")) print("Day of week: ", datetime.date.today().strftime("%A"))
Output-
Current date and time: 2019-07-24 22:49:23.129764 Current year: 2019 Month of year: July Week number of the year: 29 Weekday of the week: 3 Day of year: 205 Day of the month : 24 Day of week: Wednesday
Python isleap(year) Method Example
The below program will check if a year is a leap year or not in Python.
import calendar y=int(input("Enter any year")) if calendar.isleap(y): print("Given year is leap year=",y) else: print("Given year is not leap year=",y);
Output-
Enter any year 2010 Given year is not leap year= 2010
Example-
How do we calculate the number of days between two dates using Python
from datetime import date d1 = date(2017, 7, 8) d2 = date(2017, 9, 6) difference= d2 - d1 print ("No of Days in between=",difference.days)
Output-
No of Days in between = 60
Python Program to calculate year,month and date
from datetime import datetime def diff_month(d1, d2): print("year=",(d1.year - d2.year)) print("month=", (d1.month - d2.month)) print("days=",(d1.day - d2.day)) diff_month(datetime(2018,9,24), datetime(1994,8,6))
Output-
year= 24 month= 1 days= 18
Python program to print yesterday, today, tomorrow
The below program will help you to print the date of yesterday, today and tomorrow.
import datetime date_today1 = datetime.date.today() date_yesterday1 = date_today1 - datetime.timedelta(days = 1)date date_tomorrow1 = date_today1 + datetime.timedelta(days = 1) print('Yesterday date: ',date_yesterday1) print('Today date: ',date_today1) print('Tomorrow date: ',date_tomorrow1)
Output-
Yesterday date: 2019-07-31 Today date: 2019-08-01 Tomorrow date: 2019-08-02
Also read,
Get the current date and time in every possible format in Python
Leave a Reply