Converting streamlit library time input to milliseconds since epoch

20 views Asked by At

How to convert input from a streamlit time input widget to milliseconds since Unix Epoch? Time of the day comes from the time input widget and date have in the following string format

day = '20240325' #YYYYMMDD
time = st.time_input('Execution time', step=0:1:00)
print(time_in_ms_since_epoch)
1

There are 1 answers

0
Muhammed Samed Özmen On

U need multipy total seconds with 1000 to get Unix Epoch. I wrote simply code I hope u can understand.

import streamlit as st
from datetime import datetime, timezone, timedelta


day = '20240325' #YYYYMMDD
time = st.time_input('Execution time', step=60)


date_time = day + f'{time}'

Conver date_time which is string to datetime.

date_time_res = datetime.strptime(date_time, '%Y%m%d%H:%M:%S')
    

Create epoch time.

epoch = datetime(2024, 3, 27, 0, 0, 0)

Unix Epoch part

time_in_ms_since_epoch = int((epoch-date_time_res).total_seconds() * 1000)

print(time_in_ms_since_epoch)