国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Python datetime模塊的使用示例

瀏覽:45日期: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轉(zhuǎn)為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轉(zhuǎn)為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秒(加負數(shù))

# -*- 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模塊的使用示例的詳細內(nèi)容,更多關(guān)于Python datetime模塊的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 泸水县| 鲁甸县| 云南省| 罗田县| 容城县| 丹寨县| 河间市| 凤台县| 高台县| 大同县| 曲周县| 县级市| 怀化市| 东台市| 济宁市| 寿阳县| 石渠县| 乌兰察布市| 盘山县| 丹寨县| 同德县| 太和县| 海阳市| 库车县| 西青区| 汝阳县| 军事| 丰镇市| 墨竹工卡县| 松江区| 绥中县| 崇信县| 射洪县| 高青县| 治多县| 阳曲县| 汕尾市| 通海县| 云和县| 临澧县| 冕宁县|