8 python time$datetime

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
 
原文地址:https://www.cnblogs.com/foremostxl/p/9502264.html