python日期与时间
来源:互联网 发布:2017淘宝法律法规纳税 编辑:程序博客网 时间:2024/06/06 18:16
Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能。
Python 提供了一个 time
和 calendar
模块可以用于格式化日期和时间。
时间元组
很多Python函数用一个元组装起来的9组数字处理时间:
引入time模块
# -*- coding: UTF-8 -*-import time; # 引入time模块
获取时间戳
每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示。
>>> print time.time()1506226561.4
时间戳单位最适于做日期运算。但是1970年之前的日期就无法以此表示了。太遥远的日期也不行,UNIX和Windows只支持到2038年。
获取当前时间
从返回浮点数的时间辍方式向时间元组转换,只要将浮点数传递给如localtime
之类的函数。
>>> print time.localtime()time.struct_time(tm_year=2017, tm_mon=9, tm_mday=24, tm_hour=12, tm_min=11, tm_sec=34, tm_wday=6, tm_yday=267, tm_isdst=0)
获取格式化的时间
根据需求选取各种格式,最简单的获取可读的时间模式的函数是asctime()
>>> localtime = time.asctime(time.localtime(time.time()))>>> print localtimeSun Sep 24 12:11:06 2017
格式化日期
可以使用 time 模块的 strftime 方法来格式化日期
>>> print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())2017-09-24 12:13:52>>> print time.strftime("%a %b %d %H:%M:%S %Y",time.localtime())Sun Sep 24 12:16:01 2017# 将格式字符串转换为时间戳>>> timestr = "Sun Sep 24 12:18:44 2017">>> print time.mktime(time.strptime(timestr, "%a %b %d %H:%M:%S %Y"))1506226724.0
python中时间日期格式化符号:
获取月日历
Calendar模块有很广泛的方法用来处理年历和月历,例如打印某月的月历
>>> print calendar.month(2017,9) September 2017Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30
Time
模块内置函数
time.altzone
返回格林威治西部的夏令时地区的偏移秒数。如果该地区在格林威治东部会返回负值(如西欧,包括英国)。对夏令时启用地区才能使用。 2 time.asctime([tupletime])
接受时间元组并返回一个可读的形式为”Tue Dec 11 18:07:14 2008”(2008年12月11日 周二18时07分14秒)的24个字符的字符串。 3 time.clock( )
用以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时,比time.time()更有用。 4 time.ctime([secs])
用相当于asctime(localtime(secs)),未给参数相当于asctime() 5 time.gmtime([secs])
接收时间辍(1970纪元后经过的浮点秒数)并返回格林威治天文时间下的时间元组t。注:t.tm_isdst始终为0 6 time.localtime([secs])
接收时间辍(1970纪元后经过的浮点秒数)并返回当地时间下的时间元组t(t.tm_isdst可取0或1,取决于当地当时是不是夏令时)。 7 time.mktime(tupletime)
接受时间元组并返回时间辍(1970纪元后经过的浮点秒数)。 8 time.sleep(secs)
推迟调用线程的运行,secs指秒数。 9 time.strftime(fmt[,tupletime])
接收以时间元组,并返回以可读字符串表示的当地时间,格式由fmt决定。 10 time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')
根据fmt的格式把一个时间字符串解析为时间元组。 11 time.time()
返回当前时间的时间戳(1970纪元后经过的浮点秒数)。 12 time.tzset()
根据环境变量TZ重新初始化时间相关设置。Time模块包含了以下2个非常重要的属性:
time.timezone
属性time.timezone是当地时区(未启动夏令时)距离格林威治的偏移秒数(>0,美洲;<=0大部分欧洲,亚洲,非洲)。 2 time.tzname
属性time.tzname包含一对根据情况的不同而不同的字符串,分别是带夏令时的本地时区名称,和不带的。日历Calendar
模块
此模块的函数都是日历相关的,例如打印某月的字符月历。
星期一是默认的每周第一天,星期天是默认的最后一天。更改设置需调用calendar.setfirstweekday()函数。模块包含了以下内置函数:
calendar.calendar(year,w=2,l=1,c=6)
返回一个多行字符串格式的year年年历,3个月一行,间隔距离为c。 每日宽度间隔为w字符。每行长度为21* W+18+2* C。l是每星期行数。 2 calendar.firstweekday()
返回当前每周起始日期的设置。默认情况下,首次载入caendar模块时返回0,即星期一。 3 calendar.isleap(year)
是闰年返回True,否则为false。 4 calendar.leapdays(y1,y2)
返回在Y1,Y2两年之间的闰年总数。 5 calendar.month(year,month,w=2,l=1)
返回一个多行字符串格式的year年month月日历,两行标题,一周一行。每日宽度间隔为w字符。每行的长度为7* w+6。l是每星期的行数。 6 calendar.monthcalendar(year,month)
返回一个整数的单层嵌套列表。每个子列表装载代表一个星期的整数。Year年month月外的日期都设为0;范围内的日子都由该月第几日表示,从1开始。 7 calendar.monthrange(year,month)
返回两个整数。第一个是该月的星期几的日期码,第二个是该月的日期码。日从0(星期一)到6(星期日);月从1到12。 8 calendar.prcal(year,w=2,l=1,c=6)
相当于 print calendar.calendar(year,w,l,c). 9 calendar.prmonth(year,month,w=2,l=1)
相当于 print calendar.calendar(year,w,l,c)。 10 calendar.setfirstweekday(weekday)
设置每周的起始日期码。0(星期一)到6(星期日)。 11 calendar.timegm(tupletime)
和time.gmtime相反:接受一个时间元组形式,返回该时刻的时间辍(1970纪元后经过的浮点秒数)。 12 calendar.weekday(year,month,day)
返回给定日期的日期码。0(星期一)到6(星期日)。月份为 1(一月) 到 12(12月)。其他相关模块
datetime
模块 pytz
模块 dateutil
模块
阅读全文
0 0
- Python 日期与时间
- python日期与时间
- python中的日期与时间
- Python格式化日期时间
- Python 日期时间格式化
- python 时间和日期
- python 日期时间计算
- Python 日期和时间
- python时间日期转换
- Python 日期和时间
- python时间日期操作
- Python 日期时间
- Python日期时间表示
- Python 日期和时间
- Python日期和时间
- python日期和时间
- python日期时间格式化
- Python日期时间函数
- 假设表达式中包含三种括号 圆括号方括号大括号 设计一个算法用顺序栈判断表达式中的括号是否正确配对
- Android开发优秀开源项目汇总(不断更新)
- 事件
- Android和H5之间的交互
- css3的新属性border-radius的用法
- python日期与时间
- Android动态加载Jar
- node路径
- CSS及JS文档中文注释代码在浏览器中显示乱码
- 正则表达式
- 读深入理解Java虚拟机
- 使用FileProvider共享文件
- 原生js实现ajax的用户简单登入
- 为你的网站配置一个小巧的框架( CodeIgniter)