Python has a built-in module called datetime that can be used to create and modify
Because the module comes built-in with Python, installation is not required, but we do need to import it at the top of a Python file.
Creating datetime Objects
datetime module has three main types available:
date: Returns a date in the year-month-day format.
time: Returns a time in the hour-minute-second format with optional microsecond and timezone information.
datetime: Returns a date and time in the year-month-day and hour-minute-second formats.
Durations between any of these types can be returned by the
- Returns the current date and timestamp.
- Returns a new object with date and time properties.
- Returns the seconds elapsed since the epoch.
- Returns a date object in the year-month-day format.
- Returns a duration, resolved to microseconds, that occurs between dates, times, and datetimes.
Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.
Learn Python on Codecademy
Computer ScienceLooking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
Includes 6 Courses
With Professional Certification