Custom Search

Friday, March 11, 2011

python Create date string and convert to datetime object

python Django Create date string and convert to datetime object

>>> from datetime import datetime

>>> today = datetime.utcnow()

>>> uptoDate = today.strftime("%Y-%m-%d %H")

>>> uptoDate <=======date String
'2011-03-11 11'

>>> year = int(uptoDate[:4])

>>> month = int(uptoDate[5:7])

>>> day = int(uptoDate[8:10])

>>> hour = int(uptoDate[11:13])

>>> formatted_uptoDate = datetime(year, month, day, hour)

>>> formatted_uptoDate <=====datetime object
datetime.datetime(2011, 3, 11, 11, 0)

No comments:

Post a Comment