Pythondex

Tutorial

Python Datetime Module

Python’s datetime module lets you work with dates and times. You can get the current date and time, create specific dates, or do date calculations.

Getting Current Date and Time

  • datetime.datetime.now() returns current date and time.
  • datetime.date.today() returns current date only.

Creating a Specific Date

Extracting Parts of a Date

Date Arithmetic

Formatting Dates

Working with Time

What You Learned

  • datetime.datetime.now() gives current date and time.
  • datetime.date.today() gives today's date.
  • You can create a specific date using datetime.date(year, month, day).
  • Extract year, month, and day from a date using .year, .month, .day.
  • timedelta allows adding or subtracting days, weeks, etc.
  • strftime() formats dates in different styles.
  • You can also extract the current time with .time().