Tutorial by Examples

Python 3.2+ has support for %z format when parsing a string into a datetime object. UTC offset in the form +HHMM or -HHMM (empty string if the object is naive). Python 3.x3.2 import datetime dt = datetime.datetime.strptime("2016-04-15T08:27:18-0500", "%Y-%m-%dT%H:%M:%S%z"...
Dates don't exist in isolation. It is common that you will need to find the amount of time between dates or determine what the date will be tomorrow. This can be accomplished using timedelta objects import datetime today = datetime.date.today() print('Today:', today) yesterday = today - date...
The datetime module contains three primary types of objects - date, time, and datetime. import datetime # Date object today = datetime.date.today() new_year = datetime.date(2017, 01, 01) #datetime.date(2017, 1, 1) # Time object noon = datetime.time(12, 0, 0) #datetime.time(12, 0) # Curr...
Sometimes you want to iterate over a range of dates from a start date to some end date. You can do it using datetime library and timedelta object: import datetime # The size of each step in days day_delta = datetime.timedelta(days=1) start_date = datetime.date.today() end_date = start_date ...
Using the dateutil library as in the previous example on parsing timezone-aware timestamps, it is also possible to parse timestamps with a specified "short" time zone name. For dates formatted with short time zone names or abbreviations, which are generally ambiguous (e.g. CST, which coul...
By default all datetime objects are naive. To make them timezone-aware, you must attach a tzinfo object, which provides the UTC offset and timezone abbreviation as a function of date and time. Fixed Offset Time Zones For time zones that are a fixed offset from UTC, in Python 3.2+, the datetime mod...
It is possible to extract a date out of a text using the dateutil parser in a "fuzzy" mode, where components of the string not recognized as being part of a date are ignored. from dateutil.parser import parse dt = parse("Today is January 1, 2047 at 8:21:00AM", fuzzy=True) pr...
To switch between time zones, you need datetime objects that are timezone-aware. from datetime import datetime from dateutil import tz utc = tz.tzutc() local = tz.tzlocal() utc_now = datetime.utcnow() utc_now # Not timezone-aware. utc_now = utc_now.replace(tzinfo=utc) utc_now # Timezon...
Python has only limited support for parsing ISO 8601 timestamps. For strptime you need to know exactly what format it is in. As a complication the stringification of a datetime is an ISO 8601 timestamp, with space as a separator and 6 digit fraction: str(datetime.datetime(2016, 7, 22, 9, 25, 59, 55...
The datetime module can convert a POSIX timestamp to a ITC datetime object. The Epoch is January 1st, 1970 midnight. import time from datetime import datetime seconds_since_epoch=time.time() #1469182681.709 utc_date=datetime.utcfromtimestamp(seconds_since_epoch) #datetime.datetime(2016, 7, 2...
Using the calendar module import calendar from datetime import date def monthdelta(date, delta): m, y = (date.month+delta) % 12, date.year + ((date.month)+delta-1) // 12 if not m: m = 12 d = min(date.day, calendar.monthrange(y, m)[1]) return date.replace(day=d,month=m, year=...
the timedelta module comes in handy to compute differences between times: from datetime import datetime, timedelta now = datetime.now() then = datetime(2016, 5, 23) # datetime.datetime(2016, 05, 23, 0, 0, 0) Specifying time is optional when creating a new datetime object delta = now-then ...
Without timezone, with microseconds from datetime import datetime datetime.now().isoformat() # Out: '2016-07-31T23:08:20.886783' With timezone, with microseconds from datetime import datetime from dateutil.tz import tzlocal datetime.now(tzlocal()).isoformat() # Out: '2016-07-31T23:09:4...

Page 1 of 1