python3 datetime模塊


from datetime import datetime


'''datetime時間模塊'''
# 拿到當前計算機的系統時間 年-月-日 時:分:秒.毫秒
print(datetime.now())
print(type(datetime.now()))
2019-11-25 23:25:46.941824
<class 'datetime.datetime'>

from datetime import datetime

# 獲取當前系統時間
now = datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")
print(now)
print(type(now))
2019-11-27 21:40:30
<class 'str'>

 
        
from datetime import datetime

# 格林尼治時間,和我們相差8小時,utc協調世界時 Coordinated Universal Time
# coordinated 協調的
# universal 通用的
d1 = datetime.utcnow()
print(d1)
2019-11-22 13:27:33.084041

from datetime import datetime

# 用指定的時間創建datetime
print(datetime(2019, 11, 22, 12, 13, 14))
print(type(datetime(2019, 11, 22, 12, 13, 14)))
2019-11-22 12:13:14
<class 'datetime.datetime'>

from datetime import datetime

# 計算時間差
d1 = datetime(2019, 11, 22, 12, 13, 14)
d2 = datetime(2019, 11, 23, 14, 43, 14)
diff = d2 - d1
# seconds只計算當天的時分秒差(單位秒)
print(diff.seconds)
# total_seconds()計算年月日時分秒差(單位秒)
print(diff.total_seconds())
9000
95400.0

from datetime import datetime # 計算時間差 d1 = datetime(2019, 11, 22, 12, 22 , 22) d2 = datetime(2019, 11, 21, 12, 22, 22) diff = d1 - d2 print(diff.seconds) # 單純從時分秒來計算 print(diff.total_seconds()) # 包括年月日計算
0
86400.0
 
        
from datetime import datetime


def get_diff_datetime(d1, d2):
    '''計算兩個格式化時間之間差了多少年月日時分秒'''
    diff = d2 - d1
    year, days = divmod(diff.days, 365)
    month, day = divmod(days, 30)
    seconds = diff.seconds
    hour, seconds2 = divmod(seconds, 3600)
    minute, second = divmod(seconds2, 60)
    return year, month, day, hour, minute, second

date1 = datetime(2018, 1, 2, 12, 30, 10)
date2 = datetime(2019, 6, 12, 18, 40, 20)
year, month, day, hour, minute, second = get_diff_datetime(date1, date2)
print(f"兩個格式化時間之間差了{year}年{month}月{day}日{hour}時{minute}分{second}秒")
兩個格式化時間之間差了1年5月11日6時10分10秒

from datetime import datetime

# 時間格式化strftime, format格式化
d = datetime(2019, 11, 22, 12, 13, 14)
print(d.strftime("%Y-%m-%d %H:%M:%S"))
print(type(d.strftime("%Y-%m-%d %H:%M:%S")))

print(d.strftime("%Y/%m/%d %H:%M:%S"))
print(type(d.strftime("%Y/%m/%d %H:%M:%S")))
2019-11-22 12:13:14
<class 'str'>
2019/11/22 12:13:14
<class 'str'>

from datetime import datetime

# 字符串轉化為時間格式strptime, parse轉化
d = "2019-11-22 12:13:14"
print(datetime.strptime(d, "%Y-%m-%d %H:%M:%S"))
print(type(datetime.strptime(d, "%Y-%m-%d %H:%M:%S")))
2019-11-22 12:13:14
<class 'datetime.datetime'>

from datetime import datetime

'''格式必須一致,不然會報錯'''

# 字符串轉化為日期時間格式
s = "2018-4-6 12:34:18"
print(datetime.strptime(s, "%Y/%m/%d %H:%M:%S"))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last) <ipython-input-2-d9390be238e2> in <module>()  5 # 字符串轉化為日期時間格式  6 s = "2018-4-6 12:34:18" ----> 7 print(datetime.strptime(s, "%Y/%m/%d %H:%M:%S")) ~\Anaconda3\lib\_strptime.py in _strptime_datetime(cls, data_string, format)  563 """Return a class cls instance based on the input string and the  564 format string.""" --> 565 tt, fraction = _strptime(data_string, format)  566 tzname, gmtoff = tt[-2:]  567 args = tt[:6] + (fraction,) ~\Anaconda3\lib\_strptime.py in _strptime(data_string, format)  360 if not found:  361 raise ValueError("time data %r does not match format %r" % --> 362 (data_string, format))  363 if len(data_string) != found.end():  364 raise ValueError("unconverted data remains: %s" % ValueError: time data '2018-4-6 12:34:18' does not match format '%Y/%m/%d %H:%M:%S' 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM