time模組 datetime模組講解

2022-06-30 16:36:17 字數 1046 閱讀 4176

2.時間轉換

時間轉換

print(time.localtime(123123123)) # 轉換為格式化物件

print(time.gmtime(123123123))

print(time.mktime(time.localtime())) #格式化物件轉化為時間戳

print(time.strftime('%y',time.localtime()))

print(time.strptime('2011-03-07','%y-%d-%m'))

print(time.asctime())

print(time.ctime())

print(time.strftime('%a %b %d %h:%m:%s %y'))

print(time.asctime(time.localtime()))

print(time.ctime(123123123))

print(time.strftime('%y-%m-%d %x'))

datetime 模組

# 獲取格式化字串形式的時間麻煩

# 時間戳與格式化時間之間的轉換麻煩

# 獲取之前或者未來的時間麻煩

import datetime

print(datetime.datetime.now())

print(datetime.datetime.fromtimestamp(1231233213))

print(datetime.datetime.now() + datetime.timedelta(days=3))

print(datetime.datetime.now() + datetime.timedelta(days=-3))

s=datetime.datetime.now()

print(s.replace(year=2020))

datetime模組 time模組

from datetime import print datetime.now 返回當天的日期和時間 today datetime.now 定義today為當天日期時間物件 print datetime.date today 返回當天的日期物件 print datetime.time today 返...

time模組和datetime模組

時間戳是從1970年1月1日0時整開始計算的秒的偏移量。即當是時,時間戳為0,再往前負增加。1,獲取當前時間戳 import time time.time 1525679844.2732,獲取給定時間戳的本地時間 當前時區 未提供時間戳則預設當前時間戳 結構化時間 元組方式 import time ...

time模組以及datetime模組

時間三種表現形式 1.時間戳 秒數 2.結構化時間 一般是給機器看的 3.格式化時間 一般是給人看的 三種時間是可以相互轉換的 時間戳表示的是從1970年1月1日00 00 00開始按秒計算到目前的偏移量,也就是計算從1970年到現在所過去的秒數 print time.time 1637839983...