1. 程式人生 > >python time datetime

python time datetime

datetime 模組

>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2019, 1, 12, 15, 43, 12, 21677)
>>> now_time = datetime.datetime.now()
>>> print(now_time)
2019-01-12 15:43:48.265809
>>> # 只能調整的欄位: weeks days hours minutes seconds
>>> print
(now_time + datetime.timedelta(weeks=2)) # 兩週後 2019-01-26 15:43:48.265809 >>> print(now_time + datetime.timedelta(weeks=-2)) # 兩週前 2018-12-29 15:43:48.265809 >>> print(now_time + datetime.timedelta(days=3)) #三天後 2019-01-15 15:43:48.265809 >>> print(now_time + datetime.timedelta(days=-3)) #
三天前 2019-01-09 15:43:48.265809 >>> print(now_time + datetime.timedelta(hours=5)) # 5小時後 2019-01-12 20:43:48.265809 >>> print(now_time + datetime.timedelta(hours=-5)) # 5小時前 2019-01-12 10:43:48.265809 >>> print(now_time + datetime.timedelta(minutes=10)) # 10分鐘後 2019-01-12 15:53:48.265809 >>> print
(now_time + datetime.timedelta(minutes=-10)) # 10分鐘前 2019-01-12 15:33:48.265809 >>> print(now_time + datetime.timedelta(seconds=-80)) # 80秒前 2019-01-12 15:42:28.265809 >>> print(now_time + datetime.timedelta(seconds=80)) # 80秒後 2019-01-12 15:45:08.265809 >>> >>> # 可以直接調整到指定的 年 月 日 時 分 秒 等 >>> current_time = datetime.datetime.now() >>> print(current_time.replace(year=1996)) 1996-01-12 15:56:21.405609 >>> print(current_time.replace(month=8)) 2019-08-12 15:56:21.405609 >>> print(current_time.replace(year=2004, month=9,day=1)) 2004-09-01 15:56:21.405609 >>> >>> print(datetime.date.fromtimestamp(1309480210)) # 將時間戳轉化成時間 2011-07-01 >>>