DEV Community 👩‍💻👨‍💻

Franz Wong
Franz Wong

Posted on

Python datetime cookbook

Table of contents

Create datetime object without timezone

from datetime import datetime

# datetime doesn't support nanosecond
dt1 = datetime(2022, 4, 25, 10, 30, 15, microsecond=500)
Enter fullscreen mode Exit fullscreen mode

Create datetime object with timezone


from datetime import datetime
# ZoneInfo is supported since Python 3.9
from zoneinfo import ZoneInfo

# datetime doesn't support nanosecond
dt1 = datetime(2022, 4, 25, 10, 30, 15, microsecond=500, tzinfo=ZoneInfo('America/New_York'))
Enter fullscreen mode Exit fullscreen mode

Create datetime object of current time

from datetime import datetime
# ZoneInfo is supported since Python 3.9
from zoneinfo import ZoneInfo

# no timezone is set to dt1
dt1 = datetime.now()

# New York timezone is set to dt2
dt2 = datetime.now(ZoneInfo('America/New_York'))
Enter fullscreen mode Exit fullscreen mode

Create datetime object of same instant with another timezone

from datetime import datetime
# ZoneInfo is supported since Python 3.9
from zoneinfo import ZoneInfo

# dt1: 2022-04-25 10:30:15 EDT
dt1 = datetime(2022, 4, 25, 10, 30, 15, 500, ZoneInfo('America/New_York'))

# dt2: 2022-04-25 23:30:15 JST
dt2 = dt1.astimezone(ZoneInfo('Asia/Tokyo'))
Enter fullscreen mode Exit fullscreen mode

Create datetime object from timestamp

import time
from datetime import datetime

current_time = time.time()
dt1 = datetime.fromtimestamp(current_time)
Enter fullscreen mode Exit fullscreen mode

Create datetime object of same time but yesterday

from datetime import datetime, timedelta

# dt1: 2022-04-25 10:30:15 EDT
dt1 = datetime(2022, 4, 25, 10, 30, 15, 500, ZoneInfo('America/New_York'))

# dt2: 2022-04-24 10:30:15 EDT
dt2 = dt1 + timedelta(days=-1)
Enter fullscreen mode Exit fullscreen mode

Format datetime object to string

from datetime import datetime

dt1 = datetime(2022, 4, 25, 10, 30, 15, 500, ZoneInfo('America/New_York'))

# dt_str1: 2022-04-25 10:30:15.000500 EDT
dt_str1 = dt1.strftime('%Y-%m-%d %H:%M:%S.%f %Z')
Enter fullscreen mode Exit fullscreen mode

Convert string without timezone to datetime object

from datetime import datetime

pattern1 = '%Y-%m-%d %H:%M:%S.%f'

dt_str1 = '2022-04-25 10:30:15.000500'
dt1 = datetime.strptime(dt_str1, pattern1)

# if microsecond field has 3 digits only, it will be treated as milliseconds
dt_str2 = '2022-04-25 14:30:15.100'
# dt2: 2022-04-25 14:30:15.100000
dt2 = datetime.strptime(dt_str2, pattern1)
Enter fullscreen mode Exit fullscreen mode

Convert string with UTC offset to datetime object

from datetime import datetime

# Use '%z' to parse utc offset
pattern1 = '%Y-%m-%d %H:%M:%S.%f %z'
dt_str1 = '2022-04-25 10:30:15.000500 -04:00'
dt1 = datetime.strptime(dt_str1, pattern1)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Stop sifting through your feed.

Find the content you want to see.

Change your feed algorithm by adjusting your experience level and give weights to the tags you follow.