Python time模組時間轉換

來源:互聯網
上載者:User

標籤:%s   序列   char   seconds   ptime   cond   資料庫   load   mktime   

擷取目前時間

擷取目前時間戳

time_now = int(time.time())  #時間戳記  # 1529461333
格式化時間
import timecurrent_time=time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))current_hour=time.strftime(‘%H‘,time.localtime(time.time()))#輸出格式: ‘2018-06-20 10:20:19‘
資料庫datetime格式支援。
current_time=time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))#在資料庫中,欄位可以是varchar而不是datetime
將時間戳記轉為時間
a=1490931508time.strftime(‘%Y-%m-%d %H:%M:%S‘, time.localtime(a))
將時間轉為時間戳記
c=‘2017-03-31 11:38:28‘d=time.strptime(c,‘%Y-%m-%d %H:%M:%S‘)time.mktime(d)
JSON序列化mysql裡datetime欄位
from bson import json_utilimport jsonjson.dumps(anObject, default=json_util.default)json.loads(aJsonString, object_hook=json_util.object_hook)
時間運算:

方法一:

        time1 = datetime.datetime.now()        time2 = datetime.datetime.now()        seconds = (time2 - time1).seconds        print ‘seconds‘,seconds

方法二:

        #先獲得時間數組格式的日期        oneDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 1))        #minutes, days, months,        #轉換為時間戳記:        timeStamp = int(time.mktime(oneDayAgo.timetuple()))          #轉換為其他字串格式:        time1 = oneDayAgo.strftime("%Y-%m-%d %H:%M:%S")        time2 =  host.update_time        if time2 > time1:            print "正常"        else:            print "未上報"

Python time模組時間轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.