濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Python datetime模塊的使用示例

Python datetime模塊的使用示例

熱門(mén)標(biāo)簽:甘肅高頻外呼系統(tǒng) 滴滴地圖標(biāo)注公司 智能電話機(jī)器人調(diào)研 400電話在線如何申請(qǐng) 杭州房產(chǎn)地圖標(biāo)注 天津塘沽區(qū)地圖標(biāo)注 地圖標(biāo)注可以遠(yuǎn)程操作嗎 如何申請(qǐng)400電話代理 江門(mén)智能電話機(jī)器人

1、獲取當(dāng)前年月日時(shí)分秒

# -*- encoding=utf-8 -*-
import datetime
now = datetime.datetime.now()
print("now:{}".format(now))
year = now.year
print("year:{}".format(year))
month = now.month
print("month:{}".format(month))
day = now.day
print("day:{}".format(day))
hour = now.hour
print("hour:{}".format(hour))
minute = now.minute
print("minute:{}".format(minute))
second = now.second
print("second:{}".format(second))

2、datetime轉(zhuǎn)為string

# -*- encoding=utf-8 -*-
import datetime
now = 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))

3、string轉(zhuǎn)為datetime

# -*- encoding=utf-8 -*-
import datetime
time_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)

4、時(shí)間相加

# -*- encoding=utf-8 -*-
import datetime
time_str = '2021-01-28 10:00:00'
time_date = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
print('原始時(shí)間:\t\t\t\t{}'.format(time_date))
add_info = datetime.timedelta(days=1, hours=2, minutes=3, seconds=4)
add_end = time_date + add_info
print('加上1天2個(gè)小時(shí)3分鐘4秒后:\t{}'.format(add_end))

 5、時(shí)間相減

①兩個(gè)時(shí)間差

# -*- encoding=utf-8 -*-
import datetime
time_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_date
print('時(shí)間差:{}'.format(time_date3))

②減去1天2個(gè)小時(shí)3分鐘4秒(加負(fù)數(shù))

# -*- encoding=utf-8 -*-
import datetime
time_str = '2021-01-28 10:00:00'
time_date = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
print('原始時(shí)間:\t\t\t\t{}'.format(time_date))
add_info = datetime.timedelta(days=-1, hours=-2, minutes=-3, seconds=-4)
add_end = time_date + add_info
print('減去1天2個(gè)小時(shí)3分鐘4秒后:\t{}'.format(add_end))

以上就是Python datetime模塊的使用示例的詳細(xì)內(nèi)容,更多關(guān)于Python datetime模塊的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • 一篇文章帶你了解python標(biāo)準(zhǔn)庫(kù)--datetime模塊
  • python標(biāo)準(zhǔn)庫(kù)之time模塊的語(yǔ)法與簡(jiǎn)單使用
  • python常見(jiàn)模塊之OS模塊和time模塊
  • Python學(xué)習(xí)之time模塊的基本使用
  • 一篇文章帶你了解python標(biāo)準(zhǔn)庫(kù)--time模塊

標(biāo)簽:德宏 東莞 臨汾 漢中 廊坊 長(zhǎng)春 河池 重慶

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python datetime模塊的使用示例》,本文關(guān)鍵詞  Python,datetime,模塊,的,使用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python datetime模塊的使用示例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Python datetime模塊的使用示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    石柱| 桐城市| 比如县| 大冶市| 松阳县| 九龙城区| 夹江县| 临洮县| 延安市| 禄丰县| 昭平县| 七台河市| 辽阳市| 东丰县| 天祝| 司法| 河间市| 庆城县| 安达市| 垦利县| 永新县| 浙江省| 成武县| 陈巴尔虎旗| 江永县| 喀喇沁旗| 来宾市| 富宁县| 鄂托克前旗| 阿拉善盟| 修武县| 青冈县| 平果县| 兴仁县| 独山县| 台南县| 龙海市| 肇州县| 曲阜市| 紫云| 凌云县|