Python Dates and Time

--

The DateTime module is useful for handling dates in Python.

1) Get Current Date

2) Get Current Date and Time

3) Create and set a Date

4) Create and set a Date and Time

5) Date String Formats

Date Format Codes

6) Output a Formatted String from a DateTime object

Use the strftime() method from datetime to create a formatted string from a datetime object.

7) Output a Date from a Formatted String

Use the strptime() method from datetime to create a datetime from a formatted string.

8) Get the difference between 2 dates

Use the “-” operator to take the difference between 2 dates.
Outputs timedelta object.

9) Add days to a date to calculate a future date

10) Handle time zones

Use the pytz module to handle time zones.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response