1、表示时间的方式

(1)时间戳

时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。

我们运行“type(time.time())”,返回的是float类型。

Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数)。

import time
print(time.time()/3600/24/365)
48.66411243168522 直接是以秒为单位:
print(time.time())
1534671737.0206249

(2)格式化的时间字符串(string_time)也就是年月日分秒这样的时间字符串 time.localtime()方法

(3)结构化时间 元组(struct_time)共九个元素

元组(struct_time)方式:struct_time元组共有9个元素,
返回struct_time的函数主要有gmtime(),
localtime(),strptime()。
下面列出这种方式元组中的几个元素: 索引(Index) 属性(Attribute) 值(Values)
0 tm_year(年) 比如2011
1 tm_mon(月) 1 - 12
2 tm_mday(日) 1 - 31
3 tm_hour(时) 0 - 23
4 tm_min(分) 0 - 59
5 tm_sec(秒) 0 - 61
6 tm_wday(weekday) 0 - 6(0表示周日)
7 tm_yday(一年中的第几天) 1 - 366
8 tm_isdst(是否是夏令时) 默认为-1

2、time模块的方法

  • time.localtime([secs]):将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
  • time.gmtime([secs]):和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。亦即格林威治天文时间,世界标准时间。
  • time.time():返回当前时间的时间戳。
  • time.mktime(t):将一个struct_time转化为时间戳。
  • time.sleep(secs):线程推迟指定的时间运行。单位为秒。
  • time.asctime([t]):把一个表示时间的元组或者struct_time表示为这种形式:'Sun Oct 1 12:04:38 2017'。如果没有参数,将会将time.localtime()作为参数传入。
  • time.ctime([secs]):把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
  • time.strftime(format[, t]):把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。

    • 举例:time.strftime("%Y-%m-%d %X", time.localtime()) #输出'2017-10-01 12:14:23'
  • time.strptime(string[, format]):把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。

    • 举例:time.strptime('2017-10-3 17:54',"%Y-%m-%d %H:%M") #输出 time.struct_time(tm_year=2017, tm_mon=10, tm_mday=3, tm_hour=17, tm_min=54, tm_sec=0, tm_wday=1, tm_yday=276, tm_isdst=-1)

(1)、time.mktime(t):将一个struct_time转化为时间戳。

Python time mktime() 函数执行与gmtime(), localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数。

如果输入的值不是一个合法的时间,将触发 OverflowError 或 ValueError。

a = time.localtime()
print(time.mktime(a)) >1534676504.0

(2)、time.sleep(secs):线程推迟指定的时间运行。单位为秒。

sleep()方法语法:time.sleep(t)  t--推迟执行的秒数

import time
print("Start:",time.ctime())
time.sleep( 5 )
print("End :",time.ctime()) >Start: Sun Aug 19 19:10:13 2018
End : Sun Aug 19 19:10:18 2018

(3)、time.asctime([t]):把一个表示时间的元组或者struct_time表示为这种形式:'Sun Oct 1 12:04:38 2017'。

如果没有参数,将会将time.localtime()作为参数传入。

print(time.asctime())
>Sun Aug 19 19:22:25 2018

(4)、time.strftime(format[, t]):把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。

举例:time.strftime("%Y-%m-%d %X", time.localtime()) #输出'2017-10-01 12:14:23'

print(time.strftime('%Y-%m-%d-%X'))
>2018-08-19-19:28:20
print(time.strftime('%Y-%m-%d %X %U'))
%U表示周
>2018-08-19 19:34:32 33

(5)、time.strptime(string[, format]):把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。

举例:time.strptime('2017-10-3 17:54',"%Y-%m-%d %H:%M") #输出 time.struct_time(tm_year=2017, tm_mon=10, tm_mday=3, tm_hour=17, tm_min=54, tm_sec=0, tm_wday=1, tm_yday=276, tm_isdst=-1)

print(time.strptime('2017-10-3 17:54',"%Y-%m-%d %H:%M"))

>time.struct_time(tm_year=2017, tm_mon=10, tm_mday=3, tm_hour=17, tm_min=54, tm_sec=0, tm_wday=1, tm_yday=276, tm_isdst=-1)

3、 datetime模块

相比于time模块,datetime模块的接口则更直观、更容易调用

datetime模块定义了下面这几个类:类不是方法不可以直接拿来用
print(datetime.date.today())>2018-8-28 datetime.date:表示日期的类。常用的属性有year, month, day;
datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond;
datetime.datetime:表示日期时间。
datetime.timedelta:表示时间间隔,即两个时间点之间的长度。
datetime.tzinfo:与时区有关的相关信息。(这里不详细充分讨论该类,感兴趣的童鞋可以参考python手册)
datetime.date:表示日期的类。常用的属性有year, month, day;
print(datetime.date(year=2018,month=8,day=18))

2018-8-18
datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond;

print(datetime.time(hour=1,minute=20))
01:20:00

(1)d=datetime.datetime.now() 返回当前的datetime日期类型

d.timestamp(),d.today(), d.year,d.timetuple()等方法可以调用

d =datetime.datetime.now()
print(d.today())
2018-08-28 14:29:55.487000

(2)datetime.date.fromtimestamp(322222) 把一个时间戳转为datetime日期类型

用datetime进行时间运算

import datetime
print(datetime.datetime.now())
print(datetime.datetime.now()+datetime.timedelta(4))#当前时间加4天
print(datetime.datetime.now()+datetime.timedelta(hours=4))#当前时间+4个小时
>

2018-08-19 21:28:43.738000
2018-08-23 21:28:43.738000
2018-08-20 01:28:43.738000

4.时间替换


d =datetime.datetime.now()
print(d.replace(year=2017))
2017-08-28 14:29:07.72200
 

8 python time$datetime的更多相关文章

  1. Python的datetime

    Python的datetime 总会用到日期格式化和字符串转成日期,贴点代码以供参考,其实API真的是很全的,可是又不知道具体的method... datetime.datetime.strftime ...

  2. python 有关datetime时间日期 以及时间戳转换

    直接上代码 其中有注释 #coding=utf-8 import time import datetime def yes_time(): #获取当前时间 now_time = datetime.da ...

  3. python的datetime模块处理时间

    python的datetime模块主要用来处理时间,里面包含很多类,包括timedelay,date,time,datetime等 开发中经常会用到模块里面的datetime类,这是一个表示日期时间的 ...

  4. 基于Python的datetime模块和time模块源码阅读分析

    目录 1 前言  2 datetime.pyi源码分步解析 2.1 头部定义源码分析 2.2 tzinfo类源码分析 2.3 date类源码分析 2.4 time类源码分析 2.5 timedelta ...

  5. Pandas Timestamp 和 python 中 datetime 的互相转换

    Pandas 的Timestamp 和 python 的 datetime,   这是两种不同的类型. 它们之间可以互相转换. refer to: https://www.jianshu.com/p/ ...

  6. 孤荷凌寒自学python第三十一天python的datetime.timedelta模块

     孤荷凌寒自学python第三十一天python的datetime.timedelta模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) datetime.timedelta模块是一个表示 ...

  7. 孤荷凌寒自学python第三十天python的datetime.datetime模块

     孤荷凌寒自学python第三十天python的datetime.datetime模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) datetime.datetime模块包含了:datet ...

  8. 孤荷凌寒自学python第二十九天python的datetime.time模块

     孤荷凌寒自学python第二十九天python的datetime.time模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) datetime.time模块是专门用来表示纯时间部分的类. ...

  9. 孤荷凌寒自学python第二十八天python的datetime.date模块

     孤荷凌寒自学python第二十八天python的datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.toordinal() 此方法将访问从公元1年1月1日至当 ...

  10. 孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块

    孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.datetime模块 dateti ...

随机推荐

  1. window.open()与window.showModalDialog

    弹出窗口两种方式:    1.window.showModalDialog:      var feature = "dialogWidth:615px;dialogHeight:505px ...

  2. A glance at C# vNext

    Contents Introduction Background A list of features Primary constructor Read only auto-properties St ...

  3. PHP中数组的各种用法

    $a = 'false';if($a){ echo '好坑';}输出好坑,得转换成布尔值才行哦. in_array $people = array("Bill", "St ...

  4. 【转】每天一个linux命令(20):find命令之exec

    原文网址:http://www.cnblogs.com/peida/archive/2012/11/14/2769248.html find是我们很常用的一个Linux命令,但是我们一般查找出来的并不 ...

  5. php生成迷宫和迷宫寻址算法实例

    较之前的终于有所改善.生成迷宫的算法和寻址算法其实是一样.只是一个用了遍历一个用了递归.参考了网上的Mike Gold的算法. <?php //zairwolf z@cot8.com heade ...

  6. java成员内部类

    java成员内部类依赖于外部类而存在,故创建内部类需要首先创建其关联的外部类. public class Test { public static void main(String args[]) { ...

  7. WPF ListView 简单的拖拽实现(转)

    首先设置ListView的AllowDrop=True:SelectionMode=Extended;并且ListView视图为GridVIew. private void listView1_Mou ...

  8. 打开Visual Studio 2012的解决方案 连接 Dynamics CRM 2011 的Connect to Dynamics CRM Server 在其工具下没有显示

    一.使用TFS 代码管理,发现Visual Studio 2012 菜单栏 工具下的Connect to Dynamics CRM Server 没有显示. 平常打开VS下的工具都会出现Connect ...

  9. Angularjs+ThinkPHP3.2.3集成微信分享JS-SDK实践

    先来看看微信分享效果: 在没有集成微信分享js-sdk前是这样的:没有摘要,缩略图任意抓取正文图片   在集成微信分享js-sdk后是这样的:标题,摘要,缩略图自定义   一.下载微信SDK开发包 下 ...

  10. 【转】ORACLE Dataguard安装

    ORACLE Dataguard安装 标签: oracledatabasearchivesql数据库list 2011-08-01 09:40 5548人阅读 评论(1) 收藏 举报  分类: ORA ...