Python datetime cookbook
Tags: python, datetime, tutorial, beginners
Published: 2022-05-28 14:53:05

## Table of contents

* [Create `datetime` object without timezone](#create-datetime-object-without-timezone)
* [Create `datetime` object with timezone](#create-datetime-object-with-timezone)
* [Create `datetime` object of current time](#create-datetime-object-of-current-time)
* [Create `datetime` object of same instant with another timezone](#create-datetime-object-of-same-instant-with-another-timezone)
* [Create `datetime` object from timestamp](#create-datetime-object-from-timestamp)
* [Create `datetime` object of same time but yesterday](#create-datetime-object-of-same-time-but-yesterday)
* [Format `datetime` object to string](#format-datetime-object-to-string)
* [Convert string without timezone to `datetime` object](#convert-string-without-timezone-to-datetime-object)
* [Convert string with UTC offset to `datetime` object](#convert-string-with-utc-offset-to-datetime-object)

## Create `datetime` object without timezone <a name="create-datetime-object-without-timezone"></a>

```python
from datetime import datetime

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

## Create `datetime` object with timezone <a name="create-datetime-object-with-timezone"></a>

```python

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'))
```

## Create `datetime` object of current time <a name="create-datetime-object-of-current-time"></a>

```python
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'))
```

## Create `datetime` object of same instant with another timezone <a name="create-datetime-object-of-same-instant-with-another-timezone"></a>

```python
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'))
```

## Create `datetime` object from timestamp <a name="create-datetime-object-from-timestamp"></a>

```python
import time
from datetime import datetime

current_time = time.time()
dt1 = datetime.fromtimestamp(current_time)
```

## Create `datetime` object of same time but yesterday <a name="create-datetime-object-of-same-time-but-yesterday"></a>

```python
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)
```

## Format `datetime` object to string <a name="format-datetime-object-to-string"></a>

```python
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')
```

## Convert string without timezone to `datetime` object <a name="convert-string-without-timezone-to-datetime-object"></a>

```python
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)
```

## Convert string with UTC offset to `datetime` object <a name="convert-string-with-utc-offset-to-datetime-object"></a>

```python
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)
```