时间戳、字符串转换

# 时间戳转日期
def process_time(row):
    timeStamp = row['sort'][0]/1000
    date_array = time.localtime(timestamp)
    date_str = time.strftime("%Y-%m-%d %H:%M:%S", date_array)
    return date_str
  
df_origin['time'] = df_origin.apply(process_time, axis=1)

# 字符串转时间戳
start_time_str = "2018-12-30 00:00:00"
end_time_str = "2018-12-31 23:59:59"

def timeStr2Stamp(time_str):
    time_dt = datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
    timestamp = int(time.mktime(time_dt.timetuple())) * 1000
    return timestamp

start_timestamp = timeStr2Stamp(start_time_str)
print('start_timestamp:', start_timestamp)
end_timestamp = timeStr2Stamp(end_time_str)
print('end_timestamp:', end_timestamp)

你可能感兴趣的:(时间戳、字符串转换)