Python Datetime Cheatsheet

The last quick Reference Guide you need…

Photo by Thought Catalog on Unsplash

Python’s datetime module is a powerful tool for working with dates and times in your code. In this blog post, we’ll go over some of the different ways you can use the datetime module to make your life easier as a programmer.

So bookmark this page. Also , do let me know what all the tricks you are aware of.

First, let’s look at how to store and manipulate dates and times. The datetime module provides classes for storing and working with dates and times, such as datetime, date, and time. You can use these classes to store a specific date and time, or the current date and time.

Here’s an example of storing the current date and time:

import datetime

now = datetime.datetime.now()
print(f"Current date and time: {now}")

Current date and time: 2022–12–22 21:20:09.754036

And here’s an example of storing a specific date and time:

import datetime

new_year = datetime.datetime(2023, 1, 1, 0, 0, 0)
print(f"New Year's Day: {new_year}")

You can also use the timedelta class to calculate the difference between two dates or times. For example:

import datetime

now = datetime.datetime.now()
new_year = datetime.datetime(2023, 1, 1, 0, 0, 0)
time_until_new_year = new_year - now
print(f"Time until New Year's Day: {time_until_new_year}")

New Year’s Day: 2023–01–01 00:00:00

In addition to storing and manipulating dates and times, the datetime module also provides tools for formatting and parsing dates and times. You can use the strftime method to format a datetime object as a string, and the strptime function to parse a string into a datetime object.

Here’s an example of formatting a datetime object as a string:

import datetime

now = datetime.datetime.now()
formatted_date_time = now.strftime("%Y-%m-%d %H:%M:%S")
print(f"Formatted date and time: {formatted_date_time}")

Formatted date and time: 2022–12–22 21:25:47

And here’s an example of parsing a string into a datetime object:

import datetime

parsed_date_time = datetime.datetime.strptime("2022-12-01 00:00:00", "%Y-%m-%d %H:%M:%S")
print(f"Parsed date and time: {parsed_date_time}")

Parsed date and time: 2022–12–01 00:00:00

Another common task when working with dates and times is handling time zones. The datetime module provides basic support for time zones, but you can also use the pytz library to make working with time zones even easier.

Here’s an example of getting the current date and time in UTC:

import datetime

utc_now = datetime.datetime.utcnow()
print(f"Current date and time in UTC: {utc_now}")

Current date and time in UTC: 2022–12–22 15:56:58.069018

And here’s an example of converting the current UTC time to a specific time zone:

import datetime
import pytz
utc_now = datetime.datetime.utcnow()

local_tz = pytz.timezone("Asia/Kolkata")
local_now = utc_now.astimezone(local_tz)
print(f"Current date and time in local time: {local_now}")

Current date and time in local time: 2022–12–22 15:58:10.139960+05:30

Extract day, month, year

import datetime

today = datetime.datetime.now()

year = today.year
month = today.month
day = today.day

print(f"Today is {day}, month - {month} & year {year}")

Today is 22, month — 12 & year 2022

Happy Learning!


Python Datetime Cheatsheet was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Anirban Banerjee

The last quick Reference Guide you need…

Photo by Thought Catalog on Unsplash

Python’s datetime module is a powerful tool for working with dates and times in your code. In this blog post, we’ll go over some of the different ways you can use the datetime module to make your life easier as a programmer.

So bookmark this page. Also , do let me know what all the tricks you are aware of.

First, let’s look at how to store and manipulate dates and times. The datetime module provides classes for storing and working with dates and times, such as datetime, date, and time. You can use these classes to store a specific date and time, or the current date and time.

Here’s an example of storing the current date and time:

import datetime

now = datetime.datetime.now()
print(f"Current date and time: {now}")
Current date and time: 2022–12–22 21:20:09.754036

And here’s an example of storing a specific date and time:

import datetime

new_year = datetime.datetime(2023, 1, 1, 0, 0, 0)
print(f"New Year's Day: {new_year}")

You can also use the timedelta class to calculate the difference between two dates or times. For example:

import datetime

now = datetime.datetime.now()
new_year = datetime.datetime(2023, 1, 1, 0, 0, 0)
time_until_new_year = new_year - now
print(f"Time until New Year's Day: {time_until_new_year}")
New Year’s Day: 2023–01–01 00:00:00

In addition to storing and manipulating dates and times, the datetime module also provides tools for formatting and parsing dates and times. You can use the strftime method to format a datetime object as a string, and the strptime function to parse a string into a datetime object.

Here’s an example of formatting a datetime object as a string:

import datetime

now = datetime.datetime.now()
formatted_date_time = now.strftime("%Y-%m-%d %H:%M:%S")
print(f"Formatted date and time: {formatted_date_time}")
Formatted date and time: 2022–12–22 21:25:47

And here’s an example of parsing a string into a datetime object:

import datetime

parsed_date_time = datetime.datetime.strptime("2022-12-01 00:00:00", "%Y-%m-%d %H:%M:%S")
print(f"Parsed date and time: {parsed_date_time}")
Parsed date and time: 2022–12–01 00:00:00

Another common task when working with dates and times is handling time zones. The datetime module provides basic support for time zones, but you can also use the pytz library to make working with time zones even easier.

Here’s an example of getting the current date and time in UTC:

import datetime

utc_now = datetime.datetime.utcnow()
print(f"Current date and time in UTC: {utc_now}")
Current date and time in UTC: 2022–12–22 15:56:58.069018

And here’s an example of converting the current UTC time to a specific time zone:

import datetime
import pytz
utc_now = datetime.datetime.utcnow()

local_tz = pytz.timezone("Asia/Kolkata")
local_now = utc_now.astimezone(local_tz)
print(f"Current date and time in local time: {local_now}")
Current date and time in local time: 2022–12–22 15:58:10.139960+05:30

Extract day, month, year

import datetime

today = datetime.datetime.now()

year = today.year
month = today.month
day = today.day

print(f"Today is {day}, month - {month} & year {year}")
Today is 22, month — 12 & year 2022

Happy Learning!


Python Datetime Cheatsheet was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Anirban Banerjee


Print Share Comment Cite Upload Translate Updates
APA

Anirban Banerjee | Sciencx (2023-01-19T14:51:25+00:00) Python Datetime Cheatsheet. Retrieved from https://www.scien.cx/2023/01/19/python-datetime-cheatsheet/

MLA
" » Python Datetime Cheatsheet." Anirban Banerjee | Sciencx - Thursday January 19, 2023, https://www.scien.cx/2023/01/19/python-datetime-cheatsheet/
HARVARD
Anirban Banerjee | Sciencx Thursday January 19, 2023 » Python Datetime Cheatsheet., viewed ,<https://www.scien.cx/2023/01/19/python-datetime-cheatsheet/>
VANCOUVER
Anirban Banerjee | Sciencx - » Python Datetime Cheatsheet. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/01/19/python-datetime-cheatsheet/
CHICAGO
" » Python Datetime Cheatsheet." Anirban Banerjee | Sciencx - Accessed . https://www.scien.cx/2023/01/19/python-datetime-cheatsheet/
IEEE
" » Python Datetime Cheatsheet." Anirban Banerjee | Sciencx [Online]. Available: https://www.scien.cx/2023/01/19/python-datetime-cheatsheet/. [Accessed: ]
rf:citation
» Python Datetime Cheatsheet | Anirban Banerjee | Sciencx | https://www.scien.cx/2023/01/19/python-datetime-cheatsheet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.