測試結果 擴展: 可以更改"%Y-%m-d %H:%M:%S",得到任何你想要的格式 如201907170730,對應“%Y%m%d%H%M”等等,看自己需要 【END】 ...
1 def dateTotimeStamp(date,format_string="%Y-%m-%d %H:%M:%S",digits=10): 2 ''' 3 時間字元串轉時間戳,預設10位,時間串預設格式為"%Y-%m-%d %H:%M:%S" 4 ''' 5 import time 6 try: 7 date_1 = date[:19] 8 date_2 = date[20:20+digits-10] 9 time_array = time.strptime(date_1,format_string) 10 time_stamp = int(time.mktime(time_array)) 11 if digits == 10: 12 return time_stamp 13 else: 14 time_stamp = int(time_stamp*(10 ** (digits-10))) 15 time_stamp = time_stamp+int(date_2) 16 return time_stamp 17 except Exception as e: 18 print(e)
測試: