python3 时间和日期
时间间隔以秒为单位的浮点数。特别是在时间瞬间在自1970年1月1日(纪元)12点的表示。
在Python提供一个流行时间 time 模块,提供时间的的 函数功能,并可转换表示。函数 time.time()返回当前系统时间,从1970年1月1日12点起来的秒数。
示例
#!/usr/bin/python3
import time; # This is required to include time module. ticks = time.time()
print ("Number of ticks since 12:00am, January 1, 1970:", ticks)
Number of ticks since 12:00am, January 1, 1970: 1455508609.34375
日期计算使用刻度线是很容易做到的。 然而,新纪元日期之前不能以这种形式表示。在未来的一个日期也不能表示这种方式 - 分界点是在某个时候,在 UNIX和Windows上为 2038年。
| 索引 | 字段 | 值 | 
|---|---|---|
| 0 | 4-digit year | 2016 | 
| 1 | Month | 1 to 12 | 
| 2 | Day | 1 to 31 | 
| 3 | Hour | 0 to 23 | 
| 4 | Minute | 0 to 59 | 
| 5 | Second | 0 to 61 (60 or 61 are leap-seconds) | 
| 6 | Day of Week | 0 to 6 (0 is Monday) | 
| 7 | Day of year | 1 to 366 (Julian day) | 
| 8 | Daylight savings | -1, 0, 1, -1 means library determines DST | 
示例
>>> print (time.localtime())
time.struct_time(tm_year=2016, tm_mon=3, tm_mday=15, tm_hour=9, tm_min=29, tm_sec=2, tm_wday=0, tm_yday=46, tm_isdst=0)
| 索引 | 属性 | 值 | 
|---|---|---|
| 0 | tm_year | 2016 | 
| 1 | tm_mon | 1 to 12 | 
| 2 | tm_mday | 1 to 31 | 
| 3 | tm_hour | 0 to 23 | 
| 4 | tm_min | 0 to 59 | 
| 5 | tm_sec | 0 to 61 (60 or 61 are leap-seconds) | 
| 6 | tm_wday | 0 to 6 (0 is Monday) | 
| 7 | tm_yday | 1 to 366 (Julian day) | 
| 8 | tm_isdst | -1, 0, 1, -1 means library determines DST | 
划时代浮点值从秒时刻转换生成为时间元组,浮点值传递给一个函数返回时间的元组并带上所有九个项目无效(例如,本地时间)。
#!/usr/bin/python3
import time localtime = time.localtime(time.time())
print ("Local current time :", localtime)
Local current time : time.struct_time(tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=9, tm_min=29, tm_sec=2, tm_wday=0, tm_yday=46, tm_isdst=0)
#!/usr/bin/python3
import time localtime = time.asctime( time.localtime(time.time()) )
print ("Local current time :", localtime)
Local current time : Mon Feb 15 09:34:03 2016
日历模块提供各种各样年度和月度的日历方法。在这里,我们将打印给定月份(2008年1月)的日历 -
#!/usr/bin/python3
import calendar cal = calendar.month(2016, 2)
print ("Here is the calendar:")
print (cal)
Here is the calendar:
February 2016
Mo Tu We Th Fr Sa Su
1 2 3 4 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
time模块
Python提供一个流行时间模块,为时间和表示之间转换提供了相应函数。这里是所有可用方法的列表 -
| SN | 函数及描述 | 
|---|---|
| 1 | 如果定义则在当地的DST时区偏移,在UTC西部秒数。这是负值,如果当地的MST时区是UTC以东 (在西欧,包括英国).只使用这个,如果白天不为零。 | 
| 2 | 接受时间元组,并返回一个可读24个字符的字符串,如 'Tue Dec 11 18:07:14 2008'. | 
| 3 | 返回当前CPU时间为几秒钟的浮点数。要测量计算成本的不同的方法,time.clock()的值比time.time更有用。 | 
| 4 | 类似 asctime(localtime(secs))和不带参数的 ctime()函数 | 
| 5 | 接受从新纪元用秒表示瞬间和返回用UTC表示时间元组t。注: t.tm_isdst 总是为 0 | 
| 6 | 接受从新纪元用秒表示瞬间和返回本地时间的时间元组t(t.tm_isdst 为 0 或 1, 根据DST是否通过局部规则适用于时刻秒) | 
| 7 | 接受表示为本地时间的时间元组并返回从新纪元以秒表示瞬间的一个浮点值 | 
| 8 | 线程暂停secs秒调用 | 
| 9 | time.strftime(fmt[,tupletime]) 接受表示为本地时间的时间元组瞬间,并返回表示由字符串指定 fmt 瞬间的字符串 | 
| 10 | time.strptime(str,fmt='%a %b %d %H:%M:%S %Y') 解析 str 按照格式字符串格式化并返回时间元组格式的时间 | 
| 11 | 返回当前时刻,从新纪元以来的秒的浮点数 | 
| 12 | 通过重置库例程使用时间转换规则。 环境变量TZ指定如何做到这一点 | 
| SN | 属性及说明 | 
|---|---|
| 1 | time.timezone 属性time.timezone是在本地时区以UTC的秒偏移量(不包括DST) | 
| 2 | time.tzname 属性time.tzname是一对区域设置相关的字符串 | 
calendar模块
默认情况下,日历采用星期一作为一周的第一天,周日是最后一个。如要改变这种情况,调用calendar.setfirstweekday()函数。
| SN | 方法及描述 | 
|---|---|
| 1 | calendar.calendar(year,w=2,l=1,c=6) 返回一个多行字符串使用格式化成用c空格分隔的三列逐年的日历 | 
| 2 | calendar.firstweekday( ) 返回每周工作日开始的当前设置。默认情况下,当日历被首次导入是0,这意味着星期一 | 
| 3 | calendar.isleap(year) 如果 year 是闰年返回True;否则为false | 
| 4 | calendar.leapdays(y1,y2) 返回跨越润年在范围内(y1,y2)的总数 | 
| 5 | calendar.month(year,month,w=2,l=1) 返回一个多行字符串以及逐年月月份的日历,每周一行加上两个标题行。 | 
| 6 | calendar.monthcalendar(year,month) 返回列表的列表整数。 | 
| 7 | calendar.monthrange(year,month) 返回两个整数。 | 
| 8 | calendar.prcal(year,w=2,l=1,c=6) 类似打印 calendar.calendar(year,w,l,c). | 
| 9 | calendar.prmonth(year,month,w=2,l=1) 类似打印 calendar.month(year,month,w,l). | 
| 10 | calendar.setfirstweekday(weekday) 设定每个星期的第一天工作日代码。工作日代码是0(星期一)至6(星期日) | 
| 11 | calendar.timegm(tupletime) time.gmtime反转:接受时间元组形式的时刻,并返回同一时刻作为自新纪元以来秒的浮点数 | 
| 12 | calendar.weekday(year,month,day) 返回给定日期是星期几代码。 工作日代码是0(星期一)至60(星期日);月数是1(1月)到12(12月) | 
python3 时间和日期的更多相关文章
- 【转】Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
		Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ... 
- Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
		Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ... 
- python3数字、日期和时间
		1.对数值进行取整 #使用内建的round(value,ndigits)函数来取整,ndigits指定保留的位数,在取整时会取值在偶数上,如1.25取一位会取整1.2,1.26会取整1.3 In [1 ... 
- 5.Python3标准库-日期和时间
		''' 不同于int,str,float,Python没有包含对应日期和时间的原生类型,不过提供了3个相应的模块,可以采用多种表示来管理日期和时间值 time模块由底层C库提供与时间相关的函数.它包含 ... 
- Python3.x:日期库dateutil简介
		Python3.x:日期库dateutil简介 安装 pip install python-dateutil 关于parser #字符串可以很随意,可以用时间日期的英文单词,可以用横线.逗号.空格等做 ... 
- Android随笔之——Android时间、日期相关类和方法
		今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ... 
- Lua库之时间和日期操作
		Lua库之时间和日期操作 (2010-02-07 18:41:20) 转载▼ os.time() <== 返回当前系统的日历时间os.date() <== 返回本地化的时间字符串,这里是& ... 
- date 显示或设置系统时间和日期
		显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ... 
- java时间和日期类型
		在java中,代表时间和日期的类型包括:java.util.Date和java.util.Calendar,此外,在JDBC API中还提供了3个扩展类,java.UtilDate类的子类:java. ... 
随机推荐
- 【转】Alsa音频编程【精华】
			一.前序 这里了解一下各个参数的含义以及一些基本概念. 声音是连续模拟量,计算机将它离散化之后用数字表示,就有了以下几个名词术语. 样本长度(sample):样本是记录音频数据最基本的单位,计算机对每 ... 
- ZooKeeper的学习与应用
			近期大概学习了一下ZooKeeper,本身并没有深入.LGG尝试着在虚拟机里面搭了平台,看了看一些教材,从网上到处看别人的博文并引用之,还请各位大牛们谅解我的剽窃.现总结例如以下. 1. ZooKee ... 
- linux下git使用记录1 git 提交
			linux下git使用记录1 浏览:985 发布日期:2013/08/08 分类:技术分享 在使用github的时候,不可避免的接触到了git,用他来更新项目,做版本控制.这里特别把常用的命令记录 ... 
- 图片翻页效果引出的animate.css,很好玩,多动动吧~
			有一个项目,客户需要页面翻转的效果,需要应用在合作伙伴里面的图片上,一共有43张图片,我把它做成了随机定时的转动,鼠标经过时转动: animate.css科普文章:http://www.dowebok ... 
- Mysql 复制表结构 及其表的内容
			顺便转一下Mysql复制表结构.表数据的方法: 1.复制表结构及数据到新表CREATE TABLE 新表 SELECT * FROM 旧表 这种方法会将oldtable中所有的内容都拷贝过来,当然我们 ... 
- Python3.5入门学习记录-File
			在Python中,操作文件对象使用open函数来创建,下表列出了常用的操作file的函数: 序号 方法及描述 1.file.close() 关闭文件.关闭后文件不能再进行读写操作. 2.file.fl ... 
- uva 10763  Foreign Exchange <"map" ,vector>
			Foreign Exchange Your non-profit organization (iCORE - international Confederation of Revolver Enthu ... 
- [转载]启用 VIM 中的 Python 自动补全及提示功能
			转载: http://zhongwei-leg.iteye.com/blog/941474 周围的同事不喜欢使用 VIM 写 Python 代码的原因之一就是,VIM 不能像 Visual Studi ... 
- css布局学习笔记之position属性
			position属性用于定位元素,它的几个值分别如下: 1,static static 是默认值.任意 position: static; 的元素不会被特殊的定位.一个 static 元素表示它不会被 ... 
- Linux中的ln
			在安装了wdcp或在正常使用wdcp后,如有意无意用使用了yum更新系统或安装软件,有时会直接更新安装了yum源里的apache,这时问题就来了打开所有的网站或页面,都是提示Apache欢迎页面 这个 ... 
