Python获取时间

  1. 获取当前时间
    代码:
import datetime

now = datetime.datetime.now()

print "current date and time %s" % str(now)

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 "current date and time %s" % now.strftime("%Y-%m-%d %H:%M")

输出:

current date and time 2019-01-18 14:45:03.897120
current year: 2019
current month: 1
current day: 18
current hour: 14
current minute: 45
current second: 3
current microsecond: 897120
current date and time 2019-01-18 14:45

你可能感兴趣的:(Python获取时间)