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

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

Python datetime模塊的使用示例

瀏覽:50日期: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 編程
相關文章:
主站蜘蛛池模板: 马边| 南城县| 英德市| 防城港市| 大同市| 广安市| 大埔区| 盐源县| 上思县| 邹城市| 盐边县| 金沙县| 日照市| 日土县| 舞钢市| 淳化县| 启东市| 永平县| 凤台县| 南郑县| 大兴区| 潍坊市| 浏阳市| 手游| 杭州市| 黑水县| 张家川| 辽中县| 平远县| 龙里县| 四会市| 美姑县| 宝丰县| 苍南县| 岱山县| 和龙市| 福州市| 治多县| 陆河县| 沙河市| 盈江县|