Python 中处理时间的模块有三个,datetime 、time 、calendar 融汇贯通这三个模块,才能随心所欲地用 python 处理日期和时间。
模块分类
datetime
- 获取当前日期时间(datetime 类型)
>>> import datetime >>> now = datetime.datetime.now() 或: >>> now = datetime.datetime.today() >>> now datetime.datetime(2018, 7, 26, 22, 37, 0, 511448) >>> type(now) <class 'datetime.datetime'>
- 获取当前日期(date 类型)
>>> date = datetime.datetime.now().date() >>> date datetime.date(2018, 7, 26) >>> type(date) <class 'datetime.date'>
- 获取当前时间(time 类型)
>>> time = datetime.datetime.now().time() >>> time datetime.time(22, 40, 50, 222226) >>> type(time) <class 'datetime.time'>
time
- 获取当前时间的时间戳
>>> import time >>> time.time() 1532615540.215679
- 获取本地时间
>>> time.localtime(time.time()) time.struct_time(tm_year=2018, tm_mon=7, tm_mday=26, tm_hour=22, tm_min=33, tm_sec=26, tm_wday=3, tm_yday=207, tm_isdst=0)
- 获取格式化时间
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) '2018-07-26 22:34:23'
calendar
- 获取年历
>>> import calendar >>> calendar.calendar(2018)
- 获取月历
>>> calendar.month(2018, 7)
- 获取日期为星期几(0-6 代表星期一到星期日)
>>> calendar.weekday(2017, 7, 30) 6
日期和时间计算
日期计算
- 获取明天
>>> import datetime >>> tomorrow = datetime.datetime.now() + datetime.timedelta(days=1) >>> tomorrow datetime.datetime(2018, 7, 27, 22, 45, 51, 503706)
- 获取昨天
>>> yesterday = datetime.datetime.now() - datetime.timedelta(days=1) >>> yesterday datetime.datetime(2018, 7, 25, 22, 46, 21, 865430)
- 获取本月第一天
>>> now = datetime.datetime.now() >>> datetime.date(now.year, now.month, 1) datetime.date(2018, 7, 1)
- 获取本月最后一天
>>> import calendar >>> now = datetime.datetime.now() >>> first_day_weekday, last_day_num = calendar.monthrange(now.year, now.month) >>> datetime.date(now.year, now.month, last_day_num) datetime.date(2018, 7, 31)
时间计算
- 获取一小时前
>>> import datetime >>> datetime.datetime.now() - datetime.timedelta(hours=1) datetime.datetime(2018, 7, 26, 21, 47, 28, 970671)
- 获取当天最小时间
>>> datetime.datetime.combine(datetime.date.today(), datetime.time.min) datetime.datetime(2018, 7, 26, 0, 0)
格式转换
datetime <=> date
- datetime => date
>>> datetime.datetime.now().date() datetime.date(2018, 7, 26)
- date => datetime
>>> today = datetime.date.today() >>> datetime.datetime.combine(today, datetime.time.min) datetime.datetime(2018, 7, 26, 0, 0)
datetime <=> timestamp
- datetime => timestamp
>>> import time >>> time.mktime(datetime.datetime.now().timetuple()) 1532617383.0
- timestamp => datetime
>>> datetime.datetime.fromtimestamp(1532617383.0) datetime.datetime(2018, 7, 26, 23, 3, 3)
datetime <=> string
- datetime => string
>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") '2018-07-26 23:04:41'
- string => datetime
>>> datetime.datetime.strptime("2010-01-20 17:30:15", "%Y-%m-%d %H:%M:%S") datetime.datetime(2010, 1, 20, 17, 30, 15)
datetime <=> timetuple
- datetime => timetuple
>>> datetime.datetime.now().timetuple() time.struct_time(tm_year=2018, tm_mon=7, tm_mday=26, tm_hour=23, tm_min=6, tm_sec=1, tm_wday=3, tm_yday=207, tm_isdst=-1) >>> datetime.datetime.now().timetuple().tm_year 2018
- timetuple => datetime
>>> time_tuple = datetime.datetime.now().timetuple() >>> datetime.datetime.fromtimestamp(time.mktime(time_tuple)) datetime.datetime(2018, 7, 26, 23, 6, 40)
日期和时间格式化(符号)
strftime(format[, tuple]) -> string
将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出。
序号 | 字符 | 描述 |
1 | %Y | 四位数的年份 |
2 | %m | 月份 [01-12] |
3 | %d | 天 [0-31] |
4 | %H | 小时(24小时制) [0-23] |
5 | %I | 小时(12小时制 [01-12] |
6 | %M | 分钟 [00-59] |
7 | %S | 秒 [00-59] |
>>> import datetime >>> datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') '2018-07-26 23:17:32'
原创文章,转载请注明出处:http://www.opcoder.cn/article/6/