Friday, December 25, 2009

How to get today's date in YYYYMMDD format

datetime_yyyymmdd
In [1]:
#example using the time module
import time

print time.strftime('%Y%m%d') 
20141213

In [2]:
#alternatively, an example using the datetime module
import datetime

#sets the variable "today" as the datetime object.
today = datetime.datetime.today() 
In [3]:
#today is a datetime object
today 
Out[3]:
datetime.datetime(2014, 12, 13, 12, 32, 51, 292545)
In [4]:
#print the string of the datetime object in the desired format
print today.strftime('%Y%m%d') 
20141213

1 comment:

  1. Great - thank you. Perfect for a project using Python & sqlite3 :-)

    ReplyDelete