Python Editor

Python | Lesson4 | Date Module


from datetime import datetime, timedelta

# current date and time
now = datetime.now()
print("Current date and time:", now)

# date object for a specific date
date1 = datetime(2022, 12, 25)
print("Date object for 25th December, 2022:", date1)

# date object for a specific date and time
date2 = datetime(2022, 12, 25, 14, 30, 45)
print("Date object for 25th December, 2022 2:30:45 PM:", date2)

# difference between two dates
delta = date2 - date1
print("Time delta:", delta)

# adding or subtracting time from a date
new_date = date1 + timedelta(days=5)
print("New date after adding 5 days:", new_date)


No comments:

Post a Comment