」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何將“ 2005年6月1日1:33 pm”轉換為Python DateTime對象?

如何將“ 2005年6月1日1:33 pm”轉換為Python DateTime對象?

發佈於2025-03-24
瀏覽:233

How to Convert 轉換字符串“ Jun 1 2005 1:33 pm”到dateTime

,例如“ Jun 1 2005 1:33 pm”之類的dateTime字符串,如何將它們轉換為dateTime對象?將DateTime字符串解析為DateTime對象的解決方案。通過將預期格式指定為第二個參數,dateTime.strptime可以解釋字符串並將其轉換為適當的dateTime對象:

datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')

In this instance, the provided format string follows the syntax:

'%b': Abbreviated month name

'%d': Day of the month

'%Y': Year
from datetime import datetime

datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
'%I': Hour (12-hour clock)

'%M': Minute
  • '%p': AM/PM indicator
  • By following this format string, datetime.strptime accurately parses the input string into a datetime object。
  • 從dateTime對象獲取日期對象,使用.date()方法:
  • (https://docs.python.org/3/library/datetime.html#datetime.strptime)
  • (https://docs.python.org/3/library/datetime.html#strftime-and-strptime-and-strptime-format-codes)
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3