Search notes:

Python standard library: datetime

The Python module datetime provides date and time calculations.

strptime

import datetime

date_str = '1970-08-28 22:23:24'

dt = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')

print (dt.strftime('%H:%M:%S %d.%m.%Y'))
Github repository about-python, path: /standard-library/datetime/datetime/strptime.py
Compare with the R function strptime and the Perl module DateTime::Format::Strptime.

datetime.toordinal

datetime.toordinal(…) returns the number of days since December 31, 1 BC at 00:00 in the proleptic Gregorian date:
import datetime

print(datetime.datetime.strptime('0001-01-01', '%Y-%m-%d').toordinal()) #      1

print(datetime.datetime.strptime('1582-10-04', '%Y-%m-%d').toordinal()) # 577725
print(datetime.datetime.strptime('1582-10-05', '%Y-%m-%d').toordinal()) # 577726 - inexisting date
print(datetime.datetime.strptime('1582-10-06', '%Y-%m-%d').toordinal()) # 577727 - inexisting date

print(datetime.datetime.strptime('1582-10-15', '%Y-%m-%d').toordinal()) # 577736 - 1st gregorian day
Github repository about-python, path: /standard-library/datetime/datetime/toordinal.py

datetime.date

import datetime;


birthday_ = datetime.date(2014, 8, 28)
print ("Birthday: " + birthday_.strftime("%Y-%m-%d"))


# 

today_     = datetime.date.today()
in_a_week_ = today_ + datetime.timedelta(days = 7)

print ("The date in a week will be " + in_a_week_.strftime("%Y-%m-%d"))
Github repository about-python, path: /standard-library/datetime/date/script.py

datetime.timedelta

import datetime;

today_    = datetime.date.today()
tomorrow_ = today_ + datetime.timedelta(days = 1)

print ("The date in a week will be " + tomorrow_.strftime("%Y-%m-%d"))
Github repository about-python, path: /standard-library/datetime/timedelta/script.py

See also

The standard library dateutil provides extensions to datetime.

Index