Operations
What You'll Learn: In this tutorial, you'll discover how to perform various operations with dates and times using Python's datetime module. This includes getting the current date and time, formatting dates, comparing dates, and converting between strings and datetime objects.
1. datetime.now()
To get the current date and time, use the datetime.now() method from the datetime module.
Example Code:
import datetime
x = datetime.datetime.now()
print(x)
Output:
2024-11-19 13:04:55.684672
2. datetime Format
To format datetime objects into readable strings, use the strftime() method.
Example Code:
import datetime
x = datetime.datetime.now()
print(x.strftime("%Y-%m-%d %H:%M:%S"))
3. Compare datetime
To compare two datetime objects, you can use comparison operators.
Example Code:
import datetime
date1 = datetime.datetime(2024, 11, 19)
date2 = datetime.datetime(2023, 5, 17)
print(date1 > date2) # True
4. Convert String to datetime
To convert a string to a datetime object, use the strptime() method.
Example Code:
import datetime
date_string = "2024-11-19"
date_object = datetime.datetime.strptime(date_string, "%Y-%m-%d")
print(date_object)
5. Convert datetime to String
To convert a datetime object to a string, use the strftime() method.
Example Code:
import datetime
date_object = datetime.datetime.now()
date_string = date_object.strftime("%Y-%m-%d")
print(date_string)
6. Get Month as Number
To get the month as a number from a datetime object, use the month attribute.
Example Code:
import datetime
x = datetime.datetime.now()
print(x.month)
7. Get Month Name (Full Version)
To get the full month name from a datetime object, use the strftime() method.
Example Code:
import datetime
x = datetime.datetime.now()
print(x.strftime("%B"))
8. Get Month Name (Short Version)
To get the short month name from a datetime object, use the strftime() method.
Example Code:
import datetime
x = datetime.datetime.now()
print(x.strftime("%b"))
9. Get Day of Month
To get the day of the month from a datetime object, use the day attribute.
Example Code:
import datetime
x = datetime.datetime.now()
print(x.day)
10. Get Weekday as Number
To get the weekday as a number from a datetime object, use the weekday() method.
Example Code:
import datetime
x = datetime.datetime.now()
print(x.weekday()) # Monday is 0 and Sunday is 6
11. Get Weekday (Full Version)
To get the full weekday name from a datetime object, use the strftime() method.
Example Code:
import datetime
x = datetime.datetime.now()
print(x.strftime("%A"))
12. Get Weekday (Short Version)
To get the short weekday name from a datetime object, use the strftime() method.
Example Code:
import datetime
x = datetime.datetime.now()
print(x.strftime("%a"))
13. Get Number of Days Between Two Dates
To calculate the number of days between two dates, subtract one datetime object from another.
Example Code:
import datetime
date1 = datetime.datetime(2024, 11, 19)
date2 = datetime.datetime(2023, 11, 19)
difference = date1 - date2
print(difference.days)
14. Get Number of Seconds Between Two Dates
To calculate the number of seconds between two dates, subtract one datetime object from another and use the total_seconds() method.
Example Code:
import datetime
date1 = datetime.datetime(2024, 11, 19, 13, 0, 0)
date2 = datetime.datetime(2023, 11, 19, 13, 0, 0)
difference = date1 - date2
print(difference.total_seconds())
Try It Yourself: Fun Exercises
- Track Important Dates:
- Use the
datetimemodule to createdatetimeobjects for important dates and calculate the time remaining until those dates.
- Use the
- Create a Birthday Countdown:
- Write a program that takes your birthdate as input and calculates how many days are left until your next birthday.
Summary:
In this Python tutorial, we explored various datetime operations including getting the current date and time, formatting dates, comparing dates, converting between strings and datetime objects, and calculating differences between dates. Keep experimenting and have fun with Python!