Anyone know how to convert a datetime to epoch tim...
# suiteql
d
Anyone know how to convert a datetime to epoch timestamp ?
a
in what context?
in JS you can just do
.valueof()
to any date object
d
Using SQL
a
in sql you can do a datediff between your date and the epoch date
DATEDIFF( 'SS' , '1970-01-01 000000', yourDate)
d
DATEDIFF is not supported
a
its not? ugh
v
select (sysdate-TO_DATE('1970/01/01', 'YYYY/MM/DD'))*24*60*60 from dual
or similar
s
Copy code
SELECT (to_date(sys_extract_utc(systimestamp)) - DATE '1970-01-01') * 86400 + to_char(sys_extract_utc(systimestamp), 'SSSSS')
FROM DUAL
Alternatively, ISO8601 timestamps are simpler if that's an option:
Copy code
SELECT to_char(sys_extract_utc(systimestamp), 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
FROM DUAL
d
This is what I ended up with ...
(APPT_DATE + ((APPT_STARTTIME * 60) / 1440) - TO_DATE('1970/01/01', 'YYYY/MM/DD')) * 24 * 60 * 60
Where APPT_DATE is of type Date. and APPT_STARTTIME is of type int (and represents the hour of the day)
So, if APPT_DATE is 4/19/2024 and APPT_STARTTIME is 9 ... it produces 1713906000 (Which is Tuesday, April 23, 2024 90000 PM GMT). Shoot. Not quite right.
s
SELECT (APPT_DATE - DATE '1970-01-01') * 86400 + (TO_NUMBER(SUBSTR(TZ_OFFSET('US/Eastern'), 1, 3)) + APPT_STARTTIME) * 3600
The tz_offset is calculated from the current timezone offset, so will be an hour off if daylight saving time does not match for the date