新网站建设服务,做化妆招生宣传在那些网站可以做,肃宁县网站建设公司,网站seo优化检测Python 日期和时间
Python 程序能用很多方式处理日期和时间#xff0c;转换日期格式是一个常见的功能。
Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。
时间间隔是以秒为单位的浮点小数。
每个时间戳都以自从1970年1月1日午夜#xff08;历元…Python 日期和时间
Python 程序能用很多方式处理日期和时间转换日期格式是一个常见的功能。
Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。
时间间隔是以秒为单位的浮点小数。
每个时间戳都以自从1970年1月1日午夜历元经过了多长时间来表示。
Python 的 time 模块下有很多函数可以转换常见日期格式。如函数time.time()用于获取当前时间戳, 如下实例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-import time # 引入time模块ticks time.time()
print 当前时间戳为:, ticks以上实例输出结果
当前时间戳为: 1459994552.51时间戳单位最适于做日期运算。但是1970年之前的日期就无法以此表示了。太遥远的日期也不行UNIX和Windows只支持到2038年。
什么是时间元组
很多Python函数用一个元组装起来的9组数字处理时间: 上述也就是struct_time元组。这种结构具有如下属性
获取当前时间
从返回浮点数的时间戳方式向时间元组转换只要将浮点数传递给如localtime之类的函数。
#!/usr/bin/python
# -*- coding: UTF-8 -*-import timelocaltime time.localtime(time.time())
print 本地时间为 :, localtime以上实例输出结果
本地时间为 : time.struct_time(tm_year2016, tm_mon4, tm_mday7, tm_hour10, tm_min3, tm_sec27, tm_wday3, tm_yday98, tm_isdst0)获取格式化的时间
你可以根据需求选取各种格式但是最简单的获取可读的时间模式的函数是asctime():
#!/usr/bin/python
# -*- coding: UTF-8 -*-import timelocaltime time.asctime( time.localtime(time.time()) )
print 本地时间为 :, localtime以上实例输出结果
本地时间为 : Thu Apr 7 10:05:21 2016格式化日期
我们可以使用 time 模块的 strftime 方法来格式化日期
time.strftime(format[, t])实例演示
#!/usr/bin/python
# -*- coding: UTF-8 -*-import time# 格式化成2016-03-20 11:45:39形式
print time.strftime(%Y-%m-%d %H:%M:%S, time.localtime()) # 格式化成Sat Mar 28 22:24:24 2016形式
print time.strftime(%a %b %d %H:%M:%S %Y, time.localtime()) # 将格式字符串转换为时间戳
a Sat Mar 28 22:24:24 2016
print time.mktime(time.strptime(a,%a %b %d %H:%M:%S %Y))以上实例输出结果
2016-04-07 10:25:09
Thu Apr 07 10:25:09 2016
1459175064.0python中时间日期格式化符号
%y 两位数的年份表示00-99
%Y 四位数的年份表示000-9999
%m 月份01-12
%d 月内中的一天0-31
%H 24小时制小时数0-23
%I 12小时制小时数01-12
%M 分钟数00-59
%S 秒00-59
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天001-366
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数00-53星期天为星期的开始
%w 星期0-6星期天为星期的开始
%W 一年中的星期数00-53星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身获取某月日历
Calendar模块有很广泛的方法用来处理年历和月历例如打印某月的月历
#!/usr/bin/python
# -*- coding: UTF-8 -*-import calendarcal calendar.month(2016, 1)
print 以下输出2016年1月份的日历:
print cal以上实例输出结果
以下输出2016年1月份的日历:January 2016
Mo Tu We Th Fr Sa Su1 2 34 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31Time 模块
Time 模块包含了以下内置函数既有时间处理的也有转换时间格式的 Time模块包含了以下2个非常重要的属性
日历Calendar模块
此模块的函数都是日历相关的例如打印某月的字符月历。
星期一是默认的每周第一天星期天是默认的最后一天。更改设置需调用calendar.setfirstweekday()函数。模块包含了以下内置函数 文章转载自: http://www.morning.txjrc.cn.gov.cn.txjrc.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.zrlwl.cn.gov.cn.zrlwl.cn http://www.morning.qygfb.cn.gov.cn.qygfb.cn http://www.morning.rzmlc.cn.gov.cn.rzmlc.cn http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.rkkh.cn.gov.cn.rkkh.cn http://www.morning.kbdrq.cn.gov.cn.kbdrq.cn http://www.morning.cxtbh.cn.gov.cn.cxtbh.cn http://www.morning.ghjln.cn.gov.cn.ghjln.cn http://www.morning.lqznq.cn.gov.cn.lqznq.cn http://www.morning.i-bins.com.gov.cn.i-bins.com http://www.morning.ggnrt.cn.gov.cn.ggnrt.cn http://www.morning.aswev.com.gov.cn.aswev.com http://www.morning.sdamsm.com.gov.cn.sdamsm.com http://www.morning.aa1585.com.gov.cn.aa1585.com http://www.morning.zlcsz.cn.gov.cn.zlcsz.cn http://www.morning.ttdxn.cn.gov.cn.ttdxn.cn http://www.morning.rmyt.cn.gov.cn.rmyt.cn http://www.morning.hhpbj.cn.gov.cn.hhpbj.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.nzmhk.cn.gov.cn.nzmhk.cn http://www.morning.gcthj.cn.gov.cn.gcthj.cn http://www.morning.rfxyk.cn.gov.cn.rfxyk.cn http://www.morning.rqlzz.cn.gov.cn.rqlzz.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.srckl.cn.gov.cn.srckl.cn http://www.morning.swzpx.cn.gov.cn.swzpx.cn http://www.morning.rxlck.cn.gov.cn.rxlck.cn http://www.morning.xcxj.cn.gov.cn.xcxj.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.qkpzq.cn.gov.cn.qkpzq.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn http://www.morning.lhrcr.cn.gov.cn.lhrcr.cn http://www.morning.lkfsk.cn.gov.cn.lkfsk.cn http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.qgzmz.cn.gov.cn.qgzmz.cn http://www.morning.msxhb.cn.gov.cn.msxhb.cn http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.nqyzg.cn.gov.cn.nqyzg.cn http://www.morning.kdbcx.cn.gov.cn.kdbcx.cn http://www.morning.mdtfh.cn.gov.cn.mdtfh.cn http://www.morning.lxhny.cn.gov.cn.lxhny.cn http://www.morning.bmncq.cn.gov.cn.bmncq.cn http://www.morning.nkcfh.cn.gov.cn.nkcfh.cn http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.mgzjz.cn.gov.cn.mgzjz.cn http://www.morning.gjqwt.cn.gov.cn.gjqwt.cn http://www.morning.pqnpd.cn.gov.cn.pqnpd.cn http://www.morning.sqqdy.cn.gov.cn.sqqdy.cn http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.msbpb.cn.gov.cn.msbpb.cn http://www.morning.horihe.com.gov.cn.horihe.com http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.jwskq.cn.gov.cn.jwskq.cn http://www.morning.pmhln.cn.gov.cn.pmhln.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.xzsqb.cn.gov.cn.xzsqb.cn http://www.morning.dbdmr.cn.gov.cn.dbdmr.cn http://www.morning.hnrpk.cn.gov.cn.hnrpk.cn http://www.morning.c7513.cn.gov.cn.c7513.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.rcrnw.cn.gov.cn.rcrnw.cn http://www.morning.zdqsc.cn.gov.cn.zdqsc.cn http://www.morning.frfnb.cn.gov.cn.frfnb.cn http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.qpmwb.cn.gov.cn.qpmwb.cn http://www.morning.rkgyx.cn.gov.cn.rkgyx.cn http://www.morning.rgpsq.cn.gov.cn.rgpsq.cn http://www.morning.wynnb.cn.gov.cn.wynnb.cn