Python – Date

By | 14/12/2022

In this post, we will see some code snippets that can help us to manage date in Python.

HOW TO CONVERT A STRING IN A DATE
In this code, we will see how to convert a string in a date:

# importing the module datetime used for managing date
import datetime as ObjDate

# it is important using strip to remove all blanks
valueInput = (input("Insert a date using the format: dd/mm/yyyy \n")).strip()

# TO DO => In a real project, we must check if the input is a date

# The format used in the strftime method must be the same used in 
# the input otherwise, the system will generate en error
inputInDate = ObjDate.datetime.strptime(valueInput, "%d/%m/%Y")

print("")
print(type(valueInput))
print(type(inputInDate))

If we run the code, this will be the result:



HOW TO GET TODAY DATE
In this code, we will see how to get “today” date:

# importing the module datetime used for managing date
import datetime as ObjDate

# printing date without format
todayDate = ObjDate.datetime.now()
print(f"Today is: {todayDate}")

# printing date with format
todayDateFormated = ObjDate.datetime.strftime(todayDate, "%d/%m/%Y")
print(f"Today is: {todayDateFormated}")

If we run the code, this will be the result:



HOW TO GET DAY, MONTH, YEAR, HOURS, MINUTES AND SECONDS FROM A DATE:

# importing the module datetime used for managing date
import datetime as ObjDate

todayDate = ObjDate.datetime.now()
print(f"Today is: {todayDate}")
print(f"Day: {todayDate.day}")
print(f"Month: {todayDate.month}")
print(f"Year: {todayDate.year}")
print(f"Hours: {todayDate.hour}")
print(f"Minutes: {todayDate.minute}")
print(f"Seconds: {todayDate.second}")

If we run the code, this will be the result:



HOW TO ADD HOURS, DAYS, MONTHS AND YEARS AT A DATE
In this code, we will see how to add values in a date.
First of all, we need to install the module python-dateutil 2.8.2.
We open a browser, we go to https://pypi.org , we search python-dateutil and here we can see how to install it on our system:


Now, we can use the module for adding hours, days, months and years in a date:

# importing the module datetime used for managing date
import datetime as ObjDate
from dateutil.relativedelta import relativedelta

# printing date without format
todayDate = ObjDate.datetime.now()
print(f"Today is: {todayDate}")

# add 3 days at the todayDate variable
newDateValue = todayDate + relativedelta(days = 3)
print(f"Today + 3 days: {newDateValue}")

# add 48 hours at the todayDate variable
newDateValue = todayDate + relativedelta(hours = 48)
print(f"Today + 48 hours: {newDateValue}")

# add 6 months at the todayDate variable
newDateValue = todayDate + relativedelta(months = 6)
print(f"Today + 6 months: {newDateValue}")

# add 3 years at the todayDate variable
newDateValue = todayDate + relativedelta(years = 3)
print(f"Today + 3 years: {newDateValue}")

If we run the code, this will be the result:



HOW TO CALCULATE THE DIFFERENCE BETWEEN TWO DATES

# importing the module datetime used for managing date
import datetime as ObjDate
from dateutil.relativedelta import relativedelta

# printing date without format
todayDate = ObjDate.datetime.now()
print(f"Today is: {todayDate}")
print()
# add 6 months at the todayDate variable
newDateValue = todayDate + relativedelta(months= 6)
print(f"Today + 6 months: {newDateValue}")

# date difference
dateDifference = newDateValue - todayDate

print(f"The difference between the two dates is {dateDifference.days} days")

If we run the code, this will be the result:



Leave a Reply

Your email address will not be published. Required fields are marked *