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. ...
随机推荐
- Super Jumping! Jumping! Jumping!(dp)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- css3 在线编辑工具 连兼容都写好了
http://www.css3maker.com/index.html
- 前端--关于javascript对象
在javascript中对象是一种基本的数据类型,在数据结构上是一种散列表,可以看作是属性的无序集合,除了原始值其他一切都是对象.它可以用来表示现实世界中或者我们大脑中抽象出来的客体,这和其他面向对象 ...
- 自适应SimpsonSimpson积分
- 是什么让我想到开发NFinal
我是从01前开始就接触.net,那时.net还是1.0时代,很多东西都没有.后来.net出了2.0版本.从vs2005开始就使用Webform做网站.当时感觉.net能够拖来拖去,很厉害.参加工作后, ...
- jsp文件上传
整理了一下jsp的上传下载,由客户端到服务端,包括一些常规的业务 一客户端 先看最基本的情况 1 在表单设置multipart/form-data,通过提交键把数据和附件一次性提交的.服务器更多的是对 ...
- iOS 之 cocoapods安装与使用
我们都知道第三方库,一般使用cocoapods管理,cocoapods在我们IOS开发中有着很大的作用. 好了,现在看下它的安装步骤: 1.打开终端,输入 sudo gem install cocoa ...
- 安装ubuntu14.10系统的那些瞎折腾
前段时间自作孽,安装了ubuntu14.04的64位系统,而我的笔记本又是那种老古董,2G的内存所以装好之后各种不稳定,索性这个周末就重装一下吧,本来打算是直接装我以前的那个ubuntu12.04-i ...
- [poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Going from u to v or from v to u? Tim ...
- PK投票效果
/** *createTime:2015-07-21 *updateTime:2015-06-22 *author:刘俊 *Description:PK投票 *phone:13469119119 ** ...