time 模块

time模块方法:

  1. >>> import time
  2. >>> time.time() #时间戳 秒级别
  3. 1519212085.6211221 #从1970年到现在一共度过1519212085秒;
  4. >>> time.time()/3600/24/365 #48年 1970-2018年有这么多秒 1h=3600s
  5. 48.17390346104816
  6. >>> time.localtime() #本地时间
  7. time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=20, tm_min=8, tm_sec=33, tm_wday=2, tm_yday=52, tm_isdst=0)
  8. >>> time.localtime(2342342434)
  9. time.struct_time(tm_year=2044, tm_mon=3, tm_mday=23, tm_hour=18, tm_min=40, tm_sec=34, tm_wday=2, tm_yday=83, tm_isdst=0)
  10. >>> a=time.localtime()
  11. >>> a
  12. time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=19, tm_min=28, tm_sec=45, tm_wday=2, tm_yday=52, tm_isdst=0)
  13. >>> '%s-%s-%d'%(a.tm_year,a.tm_mon,a.tm_mday)
  14. '2018-2-21'
  15. >>> time.mktime(a) #把localtime()时间反转时间成时间戳
  16. 1519214861.0
  1. 0 tm_year #年 1970-2018
  2. 1 tm_mon #月 1-12
  3. 2 tm_mday #日 1-31
  4. 3 tm_hour #时 0-23
  5. 4 tm_min #分 0-59
  6. 5 tm_sec #秒 0-59
  7. 6 tm_wday #一周中得第几天 0-6
  8. 7 tm_yday #一年中得第几天 0-365
  9. 8 tm_isdst #是否是夏令时 0-1
  1. >>> time.gmtime() #比北京时间晚8点 UTC时区(0时区)
  2. time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=11, tm_min=50, tm_sec=46, tm_wday=2, tm_yday=52, tm_isdst=0)
  3. >>> time.sleep(2) #睡一会 单位为 秒
  4. >>> time.asctime() #表示时间得另外一种方法
  5. 'Wed Feb 21 20:53:43 2018'
  6. >>> time.ctime() #表示时间得另外一种方法
  7. 'Wed Feb 21 20:57:16 2018'
  8. >>> time.ctime(3123)
  9. 'Thu Jan 1 08:52:03 1970'
  10. >>> time.ctime(0)
  11. 'Thu Jan 1 08:00:00 1970'
  12. >>> time.strftime('%Y-%m-%d %H:%M:%S') #表示时间 字符串
  13. '2018-02-21 21:04:48'
  14. >>> a=time.localtime(2432423423)
  15. >>> a
  16. time.struct_time(tm_year=2047, tm_mon=1, tm_mday=30, tm_hour=9, tm_min=10, tm_sec=23, tm_wday=2, tm_yday=30, tm_isdst=0)
  17. >>> time.strftime('%Y-%m-%d %H:%M:%S',a) #表示某一时间戳得 字符串
  18. '2047-01-30 09:10:23'
  19. >>> time.strftime('%Y-%m-%d %H:%M:%S %a',a) #a 周几
  20. '2047-01-30 09:10:23 Wed'
  21. >>> time.strftime('%Y-%m-%d %H:%M:%S %A',a) #A 周几
  22. '2047-01-30 09:10:23 Wednesday'
  23. >>> time.strftime('%y-%m-%d %H:%M:%S %b') #b 月
  24. '18-02-21 21:22:19 Feb'
  25. >>> time.strftime('%y-%m-%d %H:%M:%S %B') #B 月
  26. '18-02-21 21:22:29 February'
  27. >>> time.strftime('%Y-%m-%d %H:%M:%S %p',a) #p 上午下午
  28. '2047-01-30 09:10:23 AM'
  29. >>> time.strftime('%Y-%m-%d %H:%M:%S %U') #U 一年得第几周
  30. '2018-02-21 21:08:15 07'
  31. >>> time.strftime('%Y-%m-%d %H:%M:%S %w') #w 一周得第几天
  32. '2018-02-21 21:09:55 3'
  33. >>> time.strftime('%y-%m-%d %H:%M:%S %z') #z 东8区时间
  34. '18-02-21 21:10:38 +0800'
  35. >> time.strftime('%Y-%m-%d %H:%M:%S %Z') #Z 时间标准
  36. '2018-02-21 21:28:22 中国标准时间'
  37.  
  38. >>> s=time.strftime('%Y-%m-%d %H:%M:%S') #时间字符串 格式 时间戳来回转换
  39. >>> s
  40. '2018-02-21 21:32:19'
  41. >>> s2= time.strptime(s,'%Y-%m-%d %H:%M:%S')
  42. >>> s2
  43. time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=21, tm_min=32, tm_sec=19, tm_wday=2, tm_yday=52, tm_isdst=-1)
  44. >>> time.mktime(s2)
  45. 1519219939.0
  1. 字符串转时间格式对应表
  2. Meaning Notes
  3. %a Locales abbreviated weekday name.
  4. %A Locales full weekday name.
  5. %b Locales abbreviated month name.
  6. %B Locales full month name.
  7. %c Locales appropriate date and time representation.
  8. %d Day of the month as a decimal number [01,31].
  9. %H Hour (24-hour clock) as a decimal number [00,23].
  10. %I Hour (12-hour clock) as a decimal number [01,12].
  11. %j Day of the year as a decimal number [001,366].
  12. %m Month as a decimal number [01,12].
  13. %M Minute as a decimal number [00,59].
  14. %p Locales equivalent of either AM or PM. (1)
  15. %S Second as a decimal number [00,61]. (2)
  16. %U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
  17. %w Weekday as a decimal number [0(Sunday),6].
  18. %W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
  19. %x Locales appropriate date representation.
  20. %X Locales appropriate time representation.
  21. %y Year without century as a decimal number [00,99].
  22. %Y Year with century as a decimal number.
  23. %z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].
  24. %Z Time zone name (no characters if no time zone exists).
  25. %% A literal '%' character.

--------------------------------------

总结:time模块
1.time.time()
2.time.localtime()
3.time.localtime(2342342434)
4.time.mktime(a)
5.time.gmtime()
6.time.sleep(2)
7.time.asctime()
8.time.ctime()
9.time.ctime(3123)
10.time.strftime('%Y-%m-%d %H:%M:%S')
11.time.strftime('%Y-%m-%d %H:%M:%S',a)
12.time.strftime('%Y-%m-%d %H:%M:%S %A',a)
13.time.strptime(s,'%Y-%m-%d %H:%M:%S')

-----------------------------------------------------------

datetime模块

相比于time模块 datetime模块得接口更直观更容易调用 重点是进行时间运算

  1. 方法:
  2. >>> a=datetime.datetime.now()
  3. >>> a
  4. datetime.datetime(2018, 2, 21, 21, 59, 57, 526811)
  5. >>> a.year
  6. 2018
  7. >>> a.timetuple()
  8. time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=21, tm_min=59, tm_sec=57, tm_wday=2, tm_yday=52, tm_isdst=-1)
  9. >>> d2=datetime.date.fromtimestamp(time.time()) #根据时间戳快速拿到年月日
  10. >>> d2
  11. datetime.date(2018, 2, 21)
  12. >>> d2.timetuple() #注意 时分秒 丢了
  13. time.struct_time(tm_year=2018, tm_mon=2, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=52, tm_isdst=-1)
  14.  
  15. 时间运算:
  16. >>> datetime.datetime.now()-datetime.timedelta(days=1)
  17. datetime.datetime(2018, 2, 20, 22, 13, 4, 891830)
  18. >>> datetime.datetime.now()-datetime.timedelta(days=3)
  19. datetime.datetime(2018, 2, 18, 22, 14, 7, 771268)
  20. >>> datetime.datetime.now()-datetime.timedelta(hours=3)
  21. datetime.datetime(2018, 2, 21, 19, 14, 33, 758609)
  22. >>> datetime.datetime.now()+datetime.timedelta(hours=3)
  23. datetime.datetime(2018, 2, 22, 1, 14, 48, 426850)
  24. >>> datetime.datetime.now()+datetime.timedelta(minutes=10)
  25. datetime.datetime(2018, 2, 21, 22, 25, 32, 615892)
  26. >>> datetime.datetime.now()+datetime.timedelta(seconds=10)
  27. datetime.datetime(2018, 2, 21, 22, 16, 29, 661140)
  28.  
  29. 时间替换:
  30. >>> s=datetime.datetime.now()
  31. >>> s
  32. datetime.datetime(2018, 2, 21, 22, 21, 34, 62949)
  33. >>> s.replace(year=2016)
  34. datetime.datetime(2016, 2, 21, 22, 21, 34, 62949)
  35. >>> s.replace(year=2016,month=8)
  36. datetime.datetime(2016, 8, 21, 22, 21, 34, 62949)
  37. >>> s.replace(year=2016,month=8,day=2)
  38. datetime.datetime(2016, 8, 2, 22, 21, 34, 62949)
calendar是一个定义了一个日历类,,封装了一个月或年的日期和星期,另外,也可以展示文本日历和HTML日历的格式输出
  1. import calendar
  2.  
  3. c = calendar.TextCalendar(calendar.SUNDAY)
  4. c.prmonth(2016, 1)
  5.  
  6. January 2016
  7. Su Mo Tu We Th Fr Sa
  8. 1 2
  9. 3 4 5 6 7 8 9
  10. 10 11 12 13 14 15 16
  11. 17 18 19 20 21 22 23
  12. 24 25 26 27 28 29 30
  13. 31

也可以返回HTML

  1. c = calendar.HTMLCalendar(calendar.SUNDAY)
  2. print(c.formatmonth(2016, 1))
运行结果
  1. <table border="0" cellpadding="0" cellspacing="0" class="month">
  2. <tr><th colspan="7" class="month">January 2016</th></tr>
  3. <tr><th class="sun">Sun</th><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th></tr>
  4. <tr><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="fri">1</td><td class="sat">2</td></tr>
  5. <tr><td class="sun">3</td><td class="mon">4</td><td class="tue">5</td><td class="wed">6</td><td class="thu">7</td><td class="fri">8</td><td class="sat">9</td></tr>
  6. <tr><td class="sun">10</td><td class="mon">11</td><td class="tue">12</td><td class="wed">13</td><td class="thu">14</td><td class="fri">15</td><td class="sat">16</td></tr>
  7. <tr><td class="sun">17</td><td class="mon">18</td><td class="tue">19</td><td class="wed">20</td><td class="thu">21</td><td class="fri">22</td><td class="sat">23</td></tr>
  8. <tr><td class="sun">24</td><td class="mon">25</td><td class="tue">26</td><td class="wed">27</td><td class="thu">28</td><td class="fri">29</td><td class="sat">30</td></tr>
  9. <tr><td class="sun">31</td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td></tr>
  10. </table>

总结:

1.方法:a=datetime.datetime.now() a.year a.timetuple() d2=datetime.date.fromtimestamp(time.time()) 
2.时间运算:datetime.datetime.now()-datetime.timedelta(days=3)
3.时间替换:s=datetime.datetime.now() s.replace(year=2016,month=8)

  1. import time
  2.  
  3. print(time.time()) #用于计算
  4. print(time.localtime(time.time()))
  5. print(time.gmtime(time.time()+28800))
  6. print(time.mktime(time.localtime())) #格式化-->时间戳
  7. print(time.strftime('%Y-%m-%d %X',time.localtime())) #格式化--》字符串时间
  8. print(time.strftime('%F %X',time.localtime()))
  9. print(time.strptime('2017-01-03 09:37:06','%Y-%m-%d %X')) #字符串--》格式化
  10. print(time.ctime())
  1. E:\Python35\python.exe E:/time复习.py
  2. 1523599614.114538
  3. time.struct_time(tm_year=2018, tm_mon=4, tm_mday=13, tm_hour=14, tm_min=6, tm_sec=54, tm_wday=4, tm_yday=103, tm_isdst=0)
  4. time.struct_time(tm_year=2018, tm_mon=4, tm_mday=13, tm_hour=14, tm_min=6, tm_sec=54, tm_wday=4, tm_yday=103, tm_isdst=0)
  5. 1523599614.0
  6. 2018-04-13 14:06:54
  7. 2018-04-13 14:06:54
  8. time.struct_time(tm_year=2017, tm_mon=1, tm_mday=3, tm_hour=9, tm_min=37, tm_sec=6, tm_wday=1, tm_yday=3, tm_isdst=-1)
  9. Fri Apr 13 14:06:54 2018
  10.  
  11. Process finished with exit code 0

Python模块 - time,datetime,calendar的更多相关文章

  1. python 时间模块(time ,datetime,calendar)

    Python中提供了时间相关的内置模块,我们主要用的是:time模块.datetime模块和calendar模块 ⽇期格式化的标准: %y 两位数的年份表示(00-99) %Y 四位数的年份表示(00 ...

  2. Python模块学习 ---- datetime

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于time模块, ...

  3. Python3之时间模块time & datetime & calendar

    一. 简介 python 提供很多方式处理日期与时间,转换日期格式是一个常见的功能. 时间元组:很多python函数用一个元组装起来的9组数字处理时间. python中时间日期格式化符号: %y 两位 ...

  4. python模块之datetime

    相比于time模块,datetime模块的接口则更直观.更容易调用 datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day: ...

  5. python模块之datetime方法详细介绍

    datetime Python提供了许多内置模块用于操作时间日期,如calendar,time,datetime,这篇文章主要是对datetime进行汇总,datetime模块的借口实现原则更加直观, ...

  6. 【转载】【Python模块】datetime

    原文地址 一.datetime模块介绍 (一).datetime模块中包含如下类: 类名 功能说明 date 日期对象,常用的属性有year, month, day time 时间对象 datetim ...

  7. python模块time&datetime&json & picle&14.logging等

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...

  8. Python模块--time&datetime

    一.Python中时间的表示方式 1.时间戳  如 1552623413.043036 2.格式化的时间字符串  如 2015-12-02 3.struct_time  是一个元组 共有九个元素 二. ...

  9. python模块:datetime

    # Stubs for datetime # NOTE: These are incomplete! import sys from typing import Optional, SupportsA ...

随机推荐

  1. ansible之一:安装与配置

    ansible特点: 1.不需要安装客户端,通过sshd去通信 2.基于模块工作,模块可以由任何语言开发 3.不仅支持命令行试用模块,也支持yaml格式得playbook 4.支持sudo 5.有提供 ...

  2. android中activity.this跟getApplicationContext的区别

    转载: http://www.myexception.cn/android/1968332.html android中activity.this和getApplicationContext的区别 在a ...

  3. IDEA 中配置JDK

    提前安装jdk,配置环境变量 一.配置jdk 1.依次点开File -->Project Structure,点击左侧标签页,点击SDKs 2.点击+号,选SDK 3.在弹出框选择jdk路径(我 ...

  4. hibernate框架学习笔记8:一对多关系案例

    两个实体类:客户与联系人,一个客户可以有多个联系人 客户类: package domain; import java.util.HashSet; import java.util.Set; //客户实 ...

  5. Redis——主从同步原理

    刚接触到Redis,首先对Redis有一个初步的了解. 开源,免费,遵守BSD协议,key-value数据库. 可以将内存中的数据保存在磁盘中,重启的时候可以再次加载使用. 多种key-value类型 ...

  6. JavaScript(第八天)【时间与日期】

    ECMAScript提供了Date类型来处理时间和日期.Date类型内置一系列获取和设置日期时间信息的方法. 一.Date类型 ECMAScript中的Date类型是在早期Java中java.util ...

  7. Beta第七天

    听说

  8. 关于from nltk.book import * 报错解决方法

    import nltk nltk.download() 在使用上面命令安装了nltk库并运行下载后,再输入from nltk.book import * 往往会出现这样的错误提示: 出现这种错误往往是 ...

  9. 1013团队Beta冲刺day7

    项目进展 李明皇 今天解决的进度 部分数据传递和使用逻辑测试 林翔 今天解决的进度 服务器端查看个人发布的action,修改已发布消息状态的action,仍在尝试使用第三方云存储功能保存图片 孙敏铭 ...

  10. 冲刺No.3

    Alpha冲刺第三天 站立式会议 项目进展 今日团队对CSS与JS的基础知识进行了应用,并对网站的UI设计进行了讨论,对数据库设计进行了进一步的探讨,基本确立了各个表单的结构和内容.分割出项目基本模块 ...