Python時間,日期,時間戳之間轉換

1 .將字符串的時間轉換爲時間戳
     方法:
         a = "2013-10-10 23:40:00"
         將其轉換爲時間數組
         import time
         timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S" )
     轉換爲時間戳:
     timeStamp = int (time.mktime(timeArray))
     timeStamp == 1381419600
 
2 .字符串格式更改
     如a = "2013-10-10 23:40:00" ,想改成 a = "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)
 
 
3 .時間戳轉換爲指定格式日期:
     方法一:
         利用localtime()轉換爲時間數組,而後格式化爲須要的格式,如
         timeStamp = 1381419600
         timeArray = time.localtime(timeStamp)
         otherStyleTime = time.strftime( "%Y-%m-%d %H:%M:%S" , timeArray)
         otherStyletime == "2013-10-10 23:40:00"
 
     方法二:
         import datetime
         timeStamp = 1381419600
         dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
         otherStyleTime = dateArray.strftime( "%Y-%m-%d %H:%M:%S" )
         otherStyletime == "2013-10-10 23:40:00"
 
4 .獲取當前時間並轉換爲指定日期格式
     方法一:
         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" )
 
5 .得到三天前的時間
     方法:
         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
 
6 .給定時間戳,計算該時間的幾天前時間:
     timeStamp = 1381419600
     先轉換爲datetime
     import datetime
     import time
     dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
     threeDayAgo = dateArray - datetime.timedelta(days = 3 )
     參考 5 ,能夠轉換爲其餘的任意格式了
 

Python 裏的tm_isdst html

 

DST 是daylight saving time, 意思是:夏令時java

在python的time, datetime模塊下,按照struct_time格式輸出時間,最後的一個tm_isdst的值就是告知是否爲夏令時。python

tm_isdst = 1 的時候表示時間是夏令時,數組

    值爲0的時候表示非夏令時dom

    值爲-1的時候表示時間不肯定是不是夏令時 post

 

獲取本月的時間戳範圍

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Henry  17607168727@163.com
import calendar
import datetime
import time


def return_time_stamp():
    # 獲取當天日期
    today = datetime.datetime.today()
    year = today.year
    month = today.month
    # 查詢當月總天數
    month_end_days = calendar.monthrange(year,month)[1]
    # 構造本月第一天文本格式時間
    first_day_str = """%s-%s-%s 00:00:00""" % (year,month,1)
    # 構造本月最後一天文本格式時間
    end_day_str = """%s-%s-%s 00:00:00""" % (year,month,month_end_days)
    # 轉成時間數組
    first_time_array = time.strptime(first_day_str, "%Y-%m-%d %H:%M:%S")
    end_time_array = time.strptime(end_day_str, "%Y-%m-%d %H:%M:%S")
    first_time_stamp = int(time.mktime(first_time_array))
    end_time_stamp = int(time.mktime(end_time_array))
    print(year,month,month_end_days,first_time_array,end_time_array,first_time_stamp,end_time_stamp)
    return [first_time_stamp,end_time_stamp]

if __name__ == '__main__':
    
    return_time_stamp()
相關文章
相關標籤/搜索