Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert date time into unix epoch value in Postgres?

How do I convert the following format to UNIX timestamps?

A value like: 01-02-2015 10:20 PM should be converted to: 1418273999000

I did try to_timestamp function but its not working for me.

like image 547
Ashish Kumar Saxena Avatar asked Jan 02 '15 10:01

Ashish Kumar Saxena


People also ask

How do I convert timestamp to epoch time?

Convert from human-readable date to epochlong epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.

What is epoch time in PostgreSQL?

YES, you can convert EPOCH to Timestamp by merely switching to the present Timestamp in PostgreSQL DBMS. EPOCH time is nothing but the number of seconds from 00:00:00 UTC on 1 January 1970. Till date, without adding the extra leap year days, this is considered. Furthermore, every day treated as 86400 seconds.

How do I convert datetime to date in PostgreSQL?

The TO_DATE function in PostgreSQL is used to converting strings into dates. Its syntax is TO_DATE(text, text) and the return type is date. The TO_TIMESTAMP function converts string data into timestamps with timezone. Its syntax is to_timestamp(text, text) .

How do I get epoch time from date?

Multiply the two dates' absolute difference by 86400 to get the Epoch Time in seconds – using the example dates above, is 319080600.


2 Answers

If your data is stored in a column called ts, in a table called data, do this:

select extract(epoch from ts) from data 
like image 196
Joe Love Avatar answered Oct 03 '22 04:10

Joe Love


To add Joe's answer, you can use date_part, i think it's syntax is clearer than 'extract'.

select date_part('epoch', ts) from data;

like image 31
haoming Avatar answered Oct 03 '22 03:10

haoming