LANGUAGE » PYTHON » PACKAGE

Datetime

Usage

python
from datetime import datetime, timedelta, timezone

Datetime

AttributesDescription
yearBetween MINYEAR (1) and MAXYEAR (9999) inclusive.
monthBetween 1 and 12 inclusive.
dayBetween 1 and the number of days in the given month of the given year.
hourIn range(24).
minuteIn range(60).
secondIn range(60).
microsecondIn range(1000000).
tzinfoTime zone information.

Generating datetime objects:

MethodDescription
now(tz)Current local date and time. Timezone defaults to None.
fromtimestamp(ts)Local date and time corresponding to the POSIX timestamp.
fromisoformat(date_string)Local date and time given a string formatted in the ISO 8601 format.
strptime(date_string, format)Local date and time given a string formatted according to the given format.

Current date and time:

python
now_utc = datetime.now(timezone.utc)
now_jst = datetime.now(timezone(timedelta(hours=9)))

Arbitrary values:

python
mydate = datetime(2018, 5, 2, 10, 30)
mydate = datetime.fromtimestamp(1525069936)
mydate = datetime.fromisoformat('2011-11-04 00:05:23.283')
mydate = datetime.strptime('2018-04-30 13:45:01', '%Y-%m-%d %H:%M:%S')

Generating string from datetime objects:

MethodDescription
isoformat(sep='T')String representing the date and time in ISO 8601 format.
strftime(format)String representing the date and time formatted according to the given format.
python
strdate = mydate.isoformat()
strdate = mydate.strftime('%Y-%m-%d_%H-%M-%S')

Timedelta

Get a date with the next month (See also dateutil.relativedelta).

python
future_date = mydate.replace(day=1) + timedelta(days=32)

Difference between dates.

python
difference = datetime.now() - datetime(2020, 1, 1)  # timedelta object
seconds = difference.total_seconds()
str(difference)  # '3:57:20'

Timezone

Replace timezone (does not change the time itself):

python
UTC = timezone.utc
JST = timezone(timedelta(hours=9))

utcdate = mydate.replace(tzinfo=UTC)
jstdate = utcdate.astimezone(JST)