Help on converting epoch timestamp to human date. #7502
-
Hello, Exploring the some website, seems the best way is to create a Function or View. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
By "convert" do you mean format for display? Should you decide to keep using integers to store timestamps, you have a few options to display them in human readable format. // in practice you would get the timestamp from your hasura backend.
const timestamp = 1630296295850
// create a date object using the timestamp
const dateObject = new Date(timestamp)
// get the human readable string
const formattedForDisplay = dateObject.toLocaleString() Alternatively, you could use views, or hasura computed fields. In both cases, you would use the Note that your timestamps seem to be in milliseconds, which is why they must be divided by 1000 before using Here is a function you could use should you go the computed field route. CREATE FUNCTION created_at_display(my_table_row my_table)
RETURNS TEXT AS $$
SELECT to_char(to_timestamp(my_table_row.created_at / 1000), 'YYYY-MM-DD HH24:MI:SS')
$$ LANGUAGE sql STABLE; |
Beta Was this translation helpful? Give feedback.
By "convert" do you mean format for display?
Or change the underlying datatype, and migrate the existing data?
Postgres has native datetime types, and you should preferably use those when storing dates, rather than integers which seems to be what you are doing here (please correct me if I am wrong). Therefore if possible I would encourage you to change your schema to use the appropriate data type.
Should you decide to keep using integers to store timestamps, you have a few options to display them in human readable format.
The simplest is to handle it on the front end: javascript date objects can be created using unix timestamps, and then displayed in a human readable format.
// in practic…