Python使用時間戳

來源:互聯網
上載者:User

標籤:

1.將字串的時間轉換為時間戳記    方法:        a = "2013-10-10 23:40:00"        將其轉換為時間數組        importtime        timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")    轉換為時間戳記:    timeStamp = int(time.mktime(timeArray))    timeStamp == 13814196002.字串格式更改    如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"     方法二:        importdatetime        timeStamp = 1381419600        dateArray = datetime.datetime.utcfromtimestamp(timeStamp)        otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")        otherStyletime == "2013-10-10 23:40:00" 4.擷取目前時間並轉換為指定日期格式    方法一:        importtime        獲得目前時間時間戳記        now = int(time.time())  ->這是時間戳記        轉換為其它日期格式,如:"%Y-%m-%d %H:%M:%S"        timeArray = time.localtime(timeStamp)        otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)     方法二:        importdatetime        獲得目前時間        now = datetime.datetime.now()  ->這是時間數組格式        轉換為指定的格式:        otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S") 5.獲得三天前的時間    方法:        importtime        importdatetime        先獲得時間數組格式的日期        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    importdatetime    importtime    dateArray = datetime.datetime.utcfromtimestamp(timeStamp)    threeDayAgo = dateArray - datetime.timedelta(days = 3)    參考5,能夠轉換為其它的隨意格式了




著作權聲明:本文部落格原創文章。部落格,未經同意,不得轉載。

Python使用時間戳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.