Posts Tagged ‘now’

Current date and time

February 25, 2011 Leave a comment
#!/usr/bin/env python

from datetime import datetime

now =
date =
time = datetime.time(now)
print "%d-%02d-%02d @ %02dh%02d" % (date.year, date.month,, time.hour, time.minute)

Sample output:

2011-02-25 @ 11h23

Update (20110523)
I wanted to use a timestamp in the name of a temporary file. Here is a slightly modified version of the code above:

print "{year}{month:02}{day:02}_{hour:02}{minute:02}{second:02}".format(year=date.year, month=date.month,, hour=time.hour, minute=time.minute, second=time.second)

Sample output:

Categories: python Tags: , , ,