defdatetime_switch_to_timestamp13(dt:str)->str:"""
dt = '2020-11-01T00:00:00.000Z'
converted result: 1604188800000
or
dt = '2021-12-31T23:59:59.999Z'
converted result: 1606780799999
"""# convert str type to <class 'datetime.datetime'> = Your time zone + (GMT+08:00)
date = datetime.strptime(dt,"%Y-%m-%dT%H:%M:%S.%fZ")+ relativedelta(hours=8)# <class 'datetime.datetime'> have the property timetuple(), then convert datetime to 10 TimestampInMillis
date_stamp =str(int(time.mktime(date.timetuple())))# <class 'datetime.datetime'> have the property microsecond, then get 3 microsecond
date_microsecond =str("%06d"% date.microsecond)[0:3]# <class 'str'> 13 TimestampInMillis = 10 TimestampInMillis + 3 microsecond
date_stamp = date_stamp + data_microsecond
return date_stamp
if __name__ =='__main__':
dt ='2020-11-01T00:00:00.000Z'print(datetime_switch_to_timestamp13(dt))# result : 1604188800000
defdatetime_switch_to_timestamp10(dt):"""
dt = 2022-01-25T10:34:48.029Z
converted result: 1642125489
"""
s = time.mktime(time.strptime(dt,"%Y-%m-%dT%H:%M:%S.%fZ"))returnint(s)if __name__ =='__main__':
dt ='2022-01-25T10:34:48.029Z'print(datetime_switch_to_timestamp13(dt))# result : 1642125489