CSC111 DateTimeExample.py
--D. Thiebaut 11:24, 12 April 2010 (UTC)
# dateTimeExample.py
# D. Thiebaut
# taken from http://www.saltycrane.com/blog/2008/06/how-to-get-current-date-and-time-in/
# shows how to get current date fields for today.
#
import datetime
now = datetime.datetime.now()
print
print "Current date and time using str method of datetime object:"
print str(now)
print
print "Current date and time using instance attributes:"
print "Current year: %d" % now.year
print "Current month: %d" % now.month
print "Current day: %d" % now.day
print "Current hour: %d" % now.hour
print "Current minute: %d" % now.minute
print "Current second: %d" % now.second
print "Current microsecond: %d" % now.microsecond
print
print "Current date and time using strftime:"
print now.strftime("%Y-%m-%d %H:%M")
Example of output:
Current date and time using str method of datetime object:
2010-04-12 07:22:34.978110
Current date and time using instance attributes:
Current year: 2010
Current month: 4
Current day: 12
Current hour: 7
Current minute: 22
Current second: 34
Current microsecond: 978110
Current date and time using strftime:
2010-04-12 07:22