Home  >  Article  >  Backend Development  >  Python中处理时间的几种方法小结

Python中处理时间的几种方法小结

WBOY
WBOYOriginal
2016-06-06 11:24:56935browse

从一个字符串开始

在CODE上查看代码片派生到我的代码片

  >>>time_str='2008-08-08 08:08:08' 


 1.1.转换为struct_time形式的时间  

在CODE上查看代码片派生到我的代码片

  >>struct = ime.strptime(time_str,'%Y-%m-%d %H:%M:%S') 
    time.struct_time(tm_year=2008, tm_mon=8, tm_mday=8, tm_hour=8, tm_min=8, tm_sec=8, tm_wday=4, tm_yday=221, tm_isdst=-1) 

1.2.如果要得到对应的时间戳(秒数):

在CODE上查看代码片派生到我的代码片

   >>>sec=time.mktime(struct) 
  >>> sec 
  1218154088.0 

 1.3.struct_time形式的时间返回开始的字符串:
在CODE上查看代码片派生到我的代码片

  >>time_str=time.strftime("%Y-%m-%d %H:%M:%S",struct) 
  >>> time_str 
  '2008-08-08 08:08:08'  

1.4.时间戳(秒数)返回到struct_time形式的时间怎么办?
在CODE上查看代码片派生到我的代码片

  <pre name="code" class="python">>> time.gmtime(sec) 
  time.struct_time(tm_year=2008, tm_mon=8, tm_mday=8, tm_hour=0, tm_min=8, tm_sec=8, tm_wday=4, tm_yday=221, tm_isdst=0) 

1.5.时间戳(秒数)要返回到字符串应该就知道怎么弄了吧?

当然,有很直接方法,不过转换回去的时间格式却不一样:
在CODE上查看代码片派生到我的代码片

  >>> time.ctime(sec) 
  'Fri Aug 08 08:08:08 2008'  

1.6.想获取当前的时间:

今天:
在CODE上查看代码片派生到我的代码片

  >>> datetime.date.today() 
      datetime.date(2015, 4, 3) 

现在:
在CODE上查看代码片派生到我的代码片

  >>> datetime.datetime.now() 
      datetime.datetime(2015, 4, 3, 15, 19, 47, 361000) 

现在的时间戳:

 >>> time.time()

1428045689.396

现在的struct_time形式时间:

>>> time.localtime()

time.struct_time(tm_year=2015, tm_mon=4, tm_mday=3, tm_hour=15, tm_min=21, tm_sec=52, tm_wday=4, tm_yday=93, tm_isdst=0)

现在的UTC日期形式:

 >>> time.ctime()

  'Fri Apr 03 15:23:45 2015'

1.7)datetime.date/datetime/time要转换成struct_time怎么办?

 >>> datetime.datetime.now().timetuple()

   time.struct_time(tm_year=2015, tm_mon=4, tm_mday=3, tm_hour=15, tm_min=31, tm_sec=19, tm_wday=4, tm_yday=93, tm_isdst=-1)

这样,结合1.2,要转换成秒是不是很简单了?

1.8.datetime.date/datetime形式的的日期怎么转换成‘2010-01-01 00:00:00'形式的字符串?

结合1.3和1.7是不是很简单?
1.9.字符串如何转换成datetime.date/datetime/time呢?
在CODE上查看代码片派生到我的代码片

  >>> datetime.datetime.strptime('2014-01-01',"%Y-%m-%d") 
     datetime.datetime(2014, 1, 1, 0, 0) 

2.0.然后要将struct_time转换为datetime.date/datetime/time也就成功了

在什么情况下需要将struct_time转换为datetime.date/datetime/time.看了2.1就明白了
2.1时间运算——时间的加减

 昨天的时间怎么算?
在CODE上查看代码片派生到我的代码片

  >> today=datetime.date.today() 

在CODE上查看代码片派生到我的代码片

  >>> delta=datetime.timedelta(days=1) 
  >>> yesterday=today-delta 
  >>> yesterday 
   datetime.date(2015, 4, 2)  


 明天呢?七天(前)后呢?一分钟前呢(),一秒呢?

看看这个构造函数:

class datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]]),能帮助你回答上面的问题吧?

注意阿,struct_time以及字符串都不可以和datetime.timedelta进行运算。所以知道从其他形式转换成datetime.date/datetime/time.是很有用的吧。

当然,struct_time也可以这样进行时间运算。比如要计算昨天:

 >>from time import time,localtime

 >>day = 24*60*60

 >>yesterday = localtime(time()-day)

2.2)时间比较:

这个就只说明一句了:datetime.(date/datetime/time.)和struct_time形式的时间都可以进行比较。(彼此之间不能比较)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn