婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Python datetime模塊的使用示例

Python datetime模塊的使用示例

熱門標簽:甘肅高頻外呼系統 滴滴地圖標注公司 智能電話機器人調研 400電話在線如何申請 杭州房產地圖標注 天津塘沽區地圖標注 地圖標注可以遠程操作嗎 如何申請400電話代理 江門智能電話機器人

1、獲取當前年月日時分秒

# -*- encoding=utf-8 -*-
import datetime
now = datetime.datetime.now()
print("now:{}".format(now))
year = now.year
print("year:{}".format(year))
month = now.month
print("month:{}".format(month))
day = now.day
print("day:{}".format(day))
hour = now.hour
print("hour:{}".format(hour))
minute = now.minute
print("minute:{}".format(minute))
second = now.second
print("second:{}".format(second))

2、datetime轉為string

# -*- encoding=utf-8 -*-
import datetime
now = datetime.datetime.now()
print('type:{}'.format(type(now)))
print('now datetime:{}'.format(now))
now_string = now.strftime('%Y-%m-%d %H:%M:%S')
print('type:{}'.format(type(now_string)))
print('now string:{}'.format(now_string))

3、string轉為datetime

# -*- encoding=utf-8 -*-
import datetime
time_str = '2021-01-28 10:51:26'
time_date = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
print('type:{}'.format(type(time_date)))
print(time_date)

4、時間相加

# -*- encoding=utf-8 -*-
import datetime
time_str = '2021-01-28 10:00:00'
time_date = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
print('原始時間:\t\t\t\t{}'.format(time_date))
add_info = datetime.timedelta(days=1, hours=2, minutes=3, seconds=4)
add_end = time_date + add_info
print('加上1天2個小時3分鐘4秒后:\t{}'.format(add_end))

 5、時間相減

①兩個時間差

# -*- encoding=utf-8 -*-
import datetime
time_str = '2021-01-28 10:00:00'
time_date = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
print('原始時間:\t{}'.format(time_date))
time_str = '2021-05-29 12:12:12'
time_date2 = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
print('原始時間2:\t{}'.format(time_date2))
time_date3 = time_date2 - time_date
print('時間差:{}'.format(time_date3))

②減去1天2個小時3分鐘4秒(加負數)

# -*- encoding=utf-8 -*-
import datetime
time_str = '2021-01-28 10:00:00'
time_date = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
print('原始時間:\t\t\t\t{}'.format(time_date))
add_info = datetime.timedelta(days=-1, hours=-2, minutes=-3, seconds=-4)
add_end = time_date + add_info
print('減去1天2個小時3分鐘4秒后:\t{}'.format(add_end))

以上就是Python datetime模塊的使用示例的詳細內容,更多關于Python datetime模塊的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • 一篇文章帶你了解python標準庫--datetime模塊
  • python標準庫之time模塊的語法與簡單使用
  • python常見模塊之OS模塊和time模塊
  • Python學習之time模塊的基本使用
  • 一篇文章帶你了解python標準庫--time模塊

標簽:德宏 東莞 臨汾 漢中 廊坊 長春 河池 重慶

巨人網絡通訊聲明:本文標題《Python datetime模塊的使用示例》,本文關鍵詞  Python,datetime,模塊,的,使用,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Python datetime模塊的使用示例》相關的同類信息!
  • 本頁收集關于Python datetime模塊的使用示例的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 乌兰浩特市| 武义县| 吉木萨尔县| 新沂市| 石门县| 军事| 咸宁市| 麻城市| 天等县| 阿鲁科尔沁旗| 南部县| 白水县| 青阳县| 西充县| 彭水| 昭平县| 永年县| 广元市| 泸定县| 温宿县| 峡江县| 桑日县| 中牟县| 凤翔县| 许昌市| 伊宁市| 都匀市| 文登市| 德州市| 北安市| 昌都县| 简阳市| 临沭县| 盘山县| 潍坊市| 广宗县| 海宁市| 长岭县| 东光县| 阜城县| 墨竹工卡县|