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")
Current date and time using str method of datetime object: 2008-06-26 11:33:15.309236 Current date and time using instance attributes: Current year: 2008 Current month: 6 Current day: 26 Current hour: 11 Current minute: 33 Current second: 15 Current microsecond: 309236 Current date and time using strftime: 2008-06-26 11:33