Python: Get current date and time

To get current date and time Python offers “datetime”. To format the output according to your needs you can use “strftime”, otherwise a month would for example be shown as 8 instead of 08.

#!/usr/bin/python
 
from datetime import datetime
 
i = datetime.now()
 
print str(i)
 
print i.strftime('%Y/%m/%d %H:%M:%S')

Sample outputs:

2015-08-13 08:15:30.495228
2015/08/13 08:15:30