python> datetime> Get the datetime (eg 20151130) format string> print today.strftime ("% Y% m% d") / print "{:% Y% m% d}" .format ( today)

I want to receive November 30, 2015 as the string 20151130.

Reference SO

There are various methods, but it seems that it can be done by the following two methods.

http://ideone.com/i1SNzk

test.py


import datetime
today = datetime.date.today()
print today

print today.strftime("%Y%m%d")
print "{:%Y%m%d}".format(today)

result


Success	time: 0.03 memory: 44680 signal:0
2015-11-30
20151130
20151130

.format () hesitates to use from the following two points.

  1. Need:
  2. Write .format (today) (opposite to today.format ())

.strftime () seems to be easier to remember.

Recommended Posts

python> datetime> Get the datetime (eg 20151130) format string> print today.strftime ("% Y% m% d") / print "{:% Y% m% d}" .format ( today)
python> datetime> yyyy / mm / dd, hh: nn: ss format print> today = datetime.datetime.today () / print today.strftime ("% Y /% m /% d,% H:% M:" % S ")
[Python 2/3] Parse the format string
python> Get yyyymmdd from date type> parsed = time.strptime (mddt) / yyyymmdd = time.strftime ("% Y% m% d", parsed)
Python string format
Python string format
Get the result in dict format with Python psycopg2
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
[Python3] Format the character string using the variable name as the key.