亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Python datetime模塊的使用示例

瀏覽:39日期:2022-06-28 14:42:24
1、獲取當前年月日時分秒

# -*- encoding=utf-8 -*-import datetimenow = datetime.datetime.now()print('now:{}'.format(now))year = now.yearprint('year:{}'.format(year))month = now.monthprint('month:{}'.format(month))day = now.dayprint('day:{}'.format(day))hour = now.hourprint('hour:{}'.format(hour))minute = now.minuteprint('minute:{}'.format(minute))second = now.secondprint('second:{}'.format(second))

Python datetime模塊的使用示例

2、datetime轉為string

# -*- encoding=utf-8 -*-import datetimenow = 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))

Python datetime模塊的使用示例

3、string轉為datetime

# -*- encoding=utf-8 -*-import datetimetime_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)

Python datetime模塊的使用示例

4、時間相加

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

Python datetime模塊的使用示例

5、時間相減

①兩個時間差

# -*- encoding=utf-8 -*-import datetimetime_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_dateprint(’時間差:{}’.format(time_date3))

Python datetime模塊的使用示例

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

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

Python datetime模塊的使用示例

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

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 好吊色综合网天天高清 | 国产精品大白天新婚身材 | 婷婷快播 | 嫩草视频在线看 | 国产激情在线观看 | 一黄色片 | 日本特黄色大片 | 久久精品天天爽夜夜爽 | 黄色大片视频在线观看 | 自拍视频在线观看视频精品 | 国产一区二区久久精品 | 久久99热这里只频精品6中文字幕 | aaa成人永久在线观看视频 | 亚洲高清在线观看看片 | 日韩免费一区 | 在线观看91香蕉国产免费 | 国产精品伦理久久久久 | 黑人巨大进入美女深处的视频 | a级毛片黄 | 免费国产视频 | 国产91精品黄网在线观看 | 色婷婷综合久久久中文字幕 | 国产高清在线观看麻豆 | 免费特级 | 性美国xxxxx免费 | 国产精品福利在线播放 | 亚洲欧美精品综合中文字幕 | 免费一级欧美片片线观看 | 全部免费特别黄的视频播放 | 欧美日韩亚洲精品国产色 | 久久国产精品亚洲综合 | 久久精品国产亚洲麻豆 | 99久久免费国产精品m9 | 日本zzzzzzz毛片 | 日韩免费播放 | 最近中国免费高清观看 | 青青爽国产手机在线观看免费 | 国产免费无遮挡精品视频 | 五月丁六月停停 | 欧美日韩a级a| 亚洲 欧美 激情 另类 校园 |