这几天收集一下时间处理的基本,先做一下笔记;谢谢分享的笔者!!!

 

  • "2013-10-10 23:40:00" -> "2013/10/10 23:40:00" {字符串的时间转换为时间戳}

 

a = "2013-10-10 23:40:00"
import time
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
timeStamp = int(time.mktime(timeArray))
print(timeStamp)

>> 1381419600
  •  

  •   "2013-10-10 23:40:00" -> "2013/10/10 23:40:00" {字符串格式修改}

 

timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)

 

  • {时间戳转换为指定格式日期}
    方法一:
        利用localtime()转换为时间数组,然后格式化为需要的格式,如
 
timeStamp = 1381419600
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

>> "2013-10-10 23:40:00"

 

 
    方法二:
        利用datetime模块
import datetime
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")

>> "2013-10-10 23:40:00"

 

 
  • {获取当前时间并转换为指定日期格式}
    方法一:
        import time
        获得当前时间时间戳
        now = int(time.time())  ->这是时间戳
        转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
        timeArray = time.localtime(timeStamp)
        otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
 
    方法二:
        import datetime
        获得当前时间
        now = datetime.datetime.now()  ->这是时间数组格式
        转换为指定的格式:
        otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")
 
  • {获得三天前的时间}
        import time
        import datetime
        先获得时间数组格式的日期
        threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3))
        转换为时间戳:
            timeStamp = int(time.mktime(threeDayAgo.timetuple()))
        转换为其他字符串格式:
            otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S")
    注:timedelta()的参数有:days,hours,seconds,microseconds
 
  • {给定时间戳,计算该时间的几天前时间}
timeStamp = 1381419600
#先转换为datetime
import datetime
import time
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
threeDayAgo = dateArray - datetime.timedelta(days = 3)

 

    参考5,可以转换为其他的任意格式了

 

posted on 2018-03-15 15:44  缪阿布  阅读(207)  评论(0编辑  收藏  举报