Python datetime模塊的使用示例
# -*- 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))
# -*- 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))
# -*- 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)
# -*- 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(’原始時(shí)間:tttt{}’.format(time_date))add_info = datetime.timedelta(days=1, hours=2, minutes=3, seconds=4)add_end = time_date + add_infoprint(’加上1天2個(gè)小時(shí)3分鐘4秒后:t{}’.format(add_end))
①兩個(gè)時(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(’原始時(shí)間: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(’原始時(shí)間2:t{}’.format(time_date2))time_date3 = time_date2 - time_dateprint(’時(shí)間差:{}’.format(time_date3))
②減去1天2個(gè)小時(shí)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(’原始時(shí)間:tttt{}’.format(time_date))add_info = datetime.timedelta(days=-1, hours=-2, minutes=-3, seconds=-4)add_end = time_date + add_infoprint(’減去1天2個(gè)小時(shí)3分鐘4秒后:t{}’.format(add_end))
以上就是Python datetime模塊的使用示例的詳細(xì)內(nèi)容,更多關(guān)于Python datetime模塊的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Nginx+php配置文件及原理解析2. 解決啟動django,瀏覽器顯示“服務(wù)器拒絕訪問”的問題3. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析4. css3溢出隱藏的方法5. python virtualenv和flask安裝沒有名為flask的模塊6. java中throws實(shí)例用法詳解7. CSS3實(shí)現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效8. Opencv+Python識別PCB板圖片的步驟9. ASP.NET MVC獲取多級類別組合下的產(chǎn)品10. 關(guān)于HTML5的img標(biāo)簽
