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

yipeiwu_com5年前Python基础

从一个字符串开始

在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形式的时间都可以进行比较。(彼此之间不能比较)

相关文章

Python numpy中矩阵的基本用法汇总

Python numpy中矩阵的基本用法汇总

Python矩阵的基本用法 mat()函数将目标数据的类型转化成矩阵(matrix) 1,mat()函数和array()函数的区别 Numpy函数库中存在两种不同的数据类型(矩阵ma...

详解PyCharm配置Anaconda的艰难心路历程

详解PyCharm配置Anaconda的艰难心路历程

在安装好pycharm后,想着anaconda中的类库会比较全,就想着将anaconda配置到pycharm中,这样可以避免以后下载各种类库。 第一步就是要下载并安装anaconda,在...

Python检查ping终端的方法

菜鸟一枚,写着试了试,虽说有点杂乱,但还是能用,我是在linux下运行的 大致说下过程: 1、把需要ping的网段中所有ip存到数组中(我是放到数组中了,其实直接for循环,一个个的也行...

Python xlwt设置excel单元格字体及格式

Python xlwt设置excel单元格字体及格式

本文根据自己初学经验编写的使用xlwt模块设置单元格的一些基本样式,如设置单元格的背景颜色,下框线,字体,字体的颜色,设置列宽行高,插入简单的图片,详细程序如下: #!/usr/bi...

基于python的七种经典排序算法(推荐)

基于python的七种经典排序算法(推荐)

一、排序的基本概念和分类 所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作。排序算法,就是如何使得记录按照要求排列的方法。 排序的稳定性: 经过某...