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模塊的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 不使用XMLHttpRequest對(duì)象實(shí)現(xiàn)Ajax效果的方法小結(jié)2. 用xslt將xml解析成xhtml的代碼3. Ajax實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)4. python實(shí)戰(zhàn)之利用pygame實(shí)現(xiàn)貪吃蛇游戲(一)5. python學(xué)習(xí)之可迭代對(duì)象、迭代器、生成器6. PHP laravel實(shí)現(xiàn)導(dǎo)出PDF功能7. php基于DOMDocument操作頁(yè)面元素實(shí)例 原創(chuàng)8. JSP靜態(tài)導(dǎo)入與動(dòng)態(tài)導(dǎo)入使用詳解9. Python建造者模式案例運(yùn)行原理解析10. python matplotlib 繪圖 和 dpi對(duì)應(yīng)關(guān)系詳解
