久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

Python datetime模塊的使用示例

瀏覽:63日期:2022-06-28 14:42:24
1、獲取當(dāng)前年月日時分秒

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

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 安乡县| 万载县| 额尔古纳市| 资源县| 康马县| 承德县| 准格尔旗| 巫溪县| 交城县| 鱼台县| 永胜县| 梧州市| 荣成市| 临洮县| 鄢陵县| 内丘县| 西乡县| 高雄市| 金堂县| 中超| 华安县| 巫溪县| 宜章县| 定日县| 宝应县| 土默特左旗| 昆明市| 天祝| 盐山县| 朝阳市| 大港区| 新沂市| 蕲春县| 高州市| 湘潭县| 铜鼓县| 积石山| 盐津县| 光山县| 海阳市| 三河市|