<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
import datetime datetime_object = datetime.datetime.now() print(datetime_object)
輸出
2022-03-29 16:36:44.749582
Process finished with exit code 0
import datetime date_object = datetime.date.today() print(date_object)
輸出
2022-03-29
Process finished with exit code 0
import datetime print(dir(datetime))
輸出
['MAXYEAR', 'MINYEAR', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']
Process finished with exit code 0
(1)範例化date物件
import datetime d = datetime.date(2022, 3, 29) print(d)
輸出
2022-03-29
Process finished with exit code 0
(2)獲取當前日期
from datetime import date today = date.today() print("當前日期 =", today)
輸出
當前日期 = 2022-03-29
Process finished with exit code 0
(3)從時間戳獲取日期
我們還可以從時間戳建立日期物件。Unix時間戳是特定日期到UTC的1970年1月1日之間的秒數。可以使用fromtimestamp()方法將時間戳轉換為日期。
from datetime import date timestamp = date.fromtimestamp(1576244364) print("日期 =", timestamp)
輸出
日期 = 2019-12-13
Process finished with exit code 0
(4)列印今天的年,月和日
from datetime import date # 今天的日期物件 today = date.today() print("當前年:", today.year) print("當前月:", today.month) print("當前日:", today.day)
輸出
當前年: 2022
當前月: 3
當前日: 29
Process finished with exit code 0
(1)從time類範例化的時間物件表示本地時間。
from datetime import time # time(hour = 0, minute = 0, second = 0) a = time() print("a =", a) # time(hour, minute and second) b = time(11, 34, 56) print("b =", b) # time(hour, minute and second) c = time(hour = 11, minute = 34, second = 56) print("c =", c) # time(hour, minute, second, microsecond) d = time(11, 34, 56, 234566) print("d =", d)
輸出
a = 00:00:00
b = 11:34:56
c = 11:34:56
d = 11:34:56.234566
Process finished with exit code 0
(2)列印時,分,秒和微秒
from datetime import time a = time(11, 34, 56) print("小時=", a.hour) print("分鐘=", a.minute) print("秒=", a.second) print("微秒=", a.microsecond)
輸出
小時= 11
分鐘= 34
秒= 56
微秒= 0
Process finished with exit code 0
(1)datetime模組有一個名為的dateclass類,可以包含來自date和time物件的資訊。
from datetime import datetime #datetime(year, month, day) a = datetime(2019, 11, 28) print(a) # datetime(year, month, day, hour, minute, second, microsecond) b = datetime(2019, 11, 28, 23, 55, 59, 342380) print(b)
輸出
2019-11-28 00:00:00
2019-11-28 23:55:59.342380
Process finished with exit code 0
(2)列印年,月,時,分和時間戳
from datetime import datetime a = datetime(2022, 3, 29, 23, 55, 59, 342380) print("年 =", a.year) print("月 =", a.month) print("日 =", a.day) print("時 =", a.hour) print("份 =", a.minute) print("時間戳 =", a.timestamp())
輸出
年 = 2022
月 = 3
日 = 29
時 = 23
份 = 55
時間戳 = 1648569359.34238
Process finished with exit code 0
(1)timedelta物件表示兩個日期或時間之間的時差。
from datetime import datetime, date t1 = date(year = 2018, month = 7, day = 12) t2 = date(year = 2017, month = 12, day = 23) t3 = t1 - t2 print("t3 =", t3) t4 = datetime(year = 2018, month = 7, day = 12, hour = 7, minute = 9, second = 33) t5 = datetime(year = 2019, month = 6, day = 10, hour = 5, minute = 55, second = 13) t6 = t4 - t5 print("t6 =", t6) print("type of t3 =", type(t3)) print("type of t6 =", type(t6))
輸出
t3 = 201 days, 0:00:00
t6 = -333 days, 1:14:20
type of t3 = <class 'datetime.timedelta'>
type of t6 = <class 'datetime.timedelta'>
Process finished with exit code 0
(2)兩個timedelta物件之間的時間差
from datetime import timedelta t1 = timedelta(weeks = 2, days = 5, hours = 1, seconds = 33) t2 = timedelta(days = 4, hours = 11, minutes = 4, seconds = 54) t3 = t1 - t2 print("t3 =", t3)
輸出
t3 = 14 days, 13:55:39
Process finished with exit code 0
到此這篇關於Python處理日期和時間的方法總結的文章就介紹到這了,更多相關Python日期時間處理內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45