python time,datetime,string轉換

  1. #把datetime轉成字符串   
  2. def datetime_toString(dt):   
  3.     return dt.strftime("%Y-%m-%d-%H")   
  4.   
  5. #把字符串轉成datetime   
  6. def string_toDatetime(string):   
  7.     return datetime.strptime(string, "%Y-%m-%d-%H")   
  8.   
  9. #把字符串轉成時間戳形式   
  10. def string_toTimestamp(strTime):   
  11.     return time.mktime(string_toDatetime(strTime).timetuple())   
  12.   
  13. #把時間戳轉成字符串形式   
  14. def timestamp_toString(stamp):   
  15.     return time.strftime("%Y-%m-%d-%H", tiem.localtime(stamp))   
  16.   
  17. #把datetime類型轉外時間戳形式   
  18. def datetime_toTimestamp(dateTim):   
  19.     return time.mktime(dateTim.timetuple())  
發佈了54 篇原創文章 · 獲贊 22 · 訪問量 44萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章