datetime contains functions and classes for working with dates and times, separately and together.
In [1]:
import datetime
t = datetime.time(1,2,3)
print(t)
print('Hour :', t.hour)
print('Minute :', t.minute)
print('Second :', t.second)
print('microseond :', t.microsecond)
print('tzinof', t.tzinfo)
In [2]:
import datetime
print('Earliest :', datetime.time.min)
print('Later :', datetime.time.max)
print('Resolution :', datetime.time.resolution)
In [3]:
import datetime
today = datetime.date.today()
print(today)
print('ctime :', today.ctime())
tt = today.timetuple()
print('tuple : tm_year =', tt.tm_year)
print(' tm_mon =', tt.tm_mon)
print(' tm_mday =', tt.tm_mday)
print(' tm_hour =', tt.tm_hour)
print(' tm_min =', tt.tm_min)
print(' tm_sec =', tt.tm_sec)
print(' tm_wday =', tt.tm_wday)
print(' tm_yday =', tt.tm_yday)
print(' tm_isdst =', tt.tm_isdst)
print('ordinal:', today.toordinal())
print('Year :', today.year)
print('Mon :', today.month)
print('Day :', today.day)
In [4]:
import datetime
import time
o = 733114
print('o :',o)
print('fromordinal(o) :', datetime.date.fromordinal(o))
In [5]:
t = time.time()
print('t :', t)
print('fromtimestamp(t)', datetime.date.fromtimestamp(t))
In [7]:
import datetime
print('Earliest:', datetime.date.min)
print('Latest:', datetime.date.max)
print('Resolution:', datetime.date.resolution)
In [8]:
import datetime
print('microseconds:', datetime.timedelta(microseconds=1))
print('milliseconds:', datetime.timedelta(milliseconds=1))
print('seconds :', datetime.timedelta(seconds=1))
print('minutes :', datetime.timedelta(minutes=1))
print('hours :', datetime.timedelta(hours=1))
print('days :', datetime.timedelta(days=1))
print('weeks :', datetime.timedelta(weeks=1))
In [9]:
import datetime
for delta in [datetime.timedelta(microseconds=1),
datetime.timedelta(milliseconds=1),
datetime.timedelta(seconds=1),
datetime.timedelta(minutes=1),
datetime.timedelta(hours=1),
datetime.timedelta(days=1),
datetime.timedelta(weeks=1),
]:
print('{:15} = {:8} seconds'.format(
str(delta), delta.total_seconds())
)
In [11]:
import datetime
today = datetime.date.today()
print('today :', today)
one_day = datetime.timedelta(days=1)
print('One day', one_day)
yesterday = today - one_day
print('Yesterday', yesterday)
tomorrow = today + one_day
print('Tomorrow', tomorrow)
In [12]:
import datetime
one_day = datetime.timedelta(days=1)
print('1 day :', one_day)
print('5 days :', one_day * 5)
print('1.5 days :', one_day * 1.5)
print('1/4 day :', one_day / 4)
# assume an hour for lunch
work_day = datetime.timedelta(hours=7)
meeting_length = datetime.timedelta(hours=1)
print('meetings per day :', work_day / meeting_length)
In [14]:
import datetime
import time
print('Times:')
t1 = datetime.time(12, 55, 0)
print(' t1:', t1)
t2 = datetime.time(13, 5, 0)
print(' t2:', t2)
print(' t1 < t2:', t1 < t2)
print()
print('Dates:')
d1 = datetime.date.today()
print(' d1:', d1)
d2 = datetime.date.today() + datetime.timedelta(days=1)
print(' d2:', d2)
print(' d1 > d2:', d1 > d2)
In [15]:
import datetime
print('Now :', datetime.datetime.now())
print('Today :', datetime.datetime.today())
print('UTC Now:', datetime.datetime.utcnow())
print()
FIELDS = [
'year', 'month', 'day',
'hour', 'minute', 'second',
'microsecond',
]
d = datetime.datetime.now()
for attr in FIELDS:
print('{:15}: {}'.format(attr, getattr(d, attr)))
In [17]:
import datetime
format = "%a %b %d %H:%M:%S %Y"
today = datetime.datetime.today()
print('ISO :', today)
s = today.strftime(format)
print('strftime:', s)
d = datetime.datetime.strptime(s, format)
print('strptime:', d.strftime(format))