本文实例讲述了python获取当前日期和时间的方法。分享给大家供大家参考。具体如下:
import datetime# Get a datetime objectnow = datetime.datetime.now()# General functions print "Year: %d" % now.yearprint "Month: %d" % now.monthprint "Day: %d" % now.dayprint "Weekday: %d" % now.weekday()# Day of week Monday = 0, Sunday = 6print "Hour: %d" % now.hourprint "Minute: %d" % now.minuteprint "Second: %d" % now.secondprint "Microsecond: %d" % now.microsecond# ISO Functionsprint "ISO Weekday: %d" % now.isoweekday()# Day of week Monday = 1, Sunday = 7print "ISO Format: %s" % now.isoformat()# ISO format, e.g. 2010-12-24T07:10:52.458593print "ISO Calendar: %s" % str(now.isocalendar())# Tuple of (ISO year, ISO week number, ISO weekday)# Formatted dateprint now.strftime("%Y/%m/%d")
希望本文所述对大家的Python程序设计有所帮助。
新闻热点
疑难解答
图片精选