python時間如何相加減,,如何比較大小

2022-10-10 03:09:11 字數 1626 閱讀 4155

##時間字串---------時間元組-------時間戳⭐※(這個是轉換之間的概念)

## 當前時間(字串2022/05/01 00:00:00)----------時間元組 (time.struct_time(tm_year=2022, tm_mon=5, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=55, tm_wday=6, tm_yday=121, tm_isdst=-1))---------時間戳(1651334455.0)

#列印當前時間  2022-04-19 20:28:23.844452

from datetime import datetime

print (datetime.now())

#列印當前時間戳  1650371373.535557    注意:時間戳是為了運算的,加減,比較大小等

import time

print(time.time())

#列印時間元組   (此時元組可以轉換成時間戳,,時間戳是運算的目的)

# time.struct_time(tm_year=2022, tm_mon=4, tm_mday=19, tm_hour=20, tm_min=30, tm_sec=49, tm_wday=1, tm_yday=109, tm_isdst=0)

print(time.localtime())

print('-----------------------')

#當前時間元組,強制轉換當前時間為年月日,時分秒格式   2022/04/19 20:37:55

print(time.strftime('%y/%m/%d %h:%m:%s',time.localtime()))

print('-----------------------')

#時間字串轉換成時間元組

#中國人習慣時間字串 2022/05/01 00:00:55 轉換成時間元組

time1='2022/05/01 00:00:55'

print(time.strptime(time1,'%y/%m/%d %h:%m:%s'))    #print(time.strptime('2022/05/01 00:00:55','%y/%m/%d %h:%m:%s'))

print('-----------------------')

#時間元組轉換為時間戳,,,目的是為了運算,,比較大小等

tt=(time.strptime('2022/05/01 00:00:55','%y/%m/%d %h:%m:%s'))

print(time.mktime(tt))

總結:當前時間元組:time.localtime()

當前時間戳: time.time()

元組轉字串:time.strftime    #2022/01/01   00:00:00

字串轉元組:time.strptime    #time.struct_time(tm_year=2022, tm_mon=5, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=55, tm_wday=6, tm_yday=121, tm_isdst=-1)

元組轉時間戳:time.mktime     #1651334455.0    

時間相加減函式

時間減1函式 create function f dateadd date datetime,datestr varchar 23 returns datetime asbegin declare bz int,s varchar 12 i int if datestr is null or dat...

oracle 時間相加減

1.分鐘轉成不同格式的時間 資料庫裡有個字段 存的分鐘 現在想顯示成hh mm格式 假設為514分鐘 select to char to date 00 00 hh24 mi 514 24 60 hh24 mi from dual 輸出結果 08 34 如果存的是秒 同理 select to cha...

js 中處理時間相加減問題

之前需求是當前時間 5天,寫法如下 1 var datacur new date 當前日期 2var datadef datacur.getfullyear datacur.getmonth 1 datacur.getdate 5 但這種寫法有個問題,就是月末比如今天 3.28 5天,就成 3.33...