简体   繁体   中英

How to compare date in PostgreSQL timestamp with time zone?

I have a field in PostgreSQL that has: "timestamp with time zone compare"

I need to use a data range comparaison from JS

var start = Date.UTC(2012,02,30);//1333065600000

var end = Date.UTC(2013,02,30); //1364601600000

that results in bigint numbers:

How to use start(1333065600000) and end (1364601600000) inside sql PostgreSQL request ?

Try something like this:

select *
from my_table
where date_field between to_timestamp(1333065600000) and to_timestamp(1364601600000);

This is essentially the same as TO_TIMESTAMP , but converts to UTC time zone if the server is not running at UTC.

SELECT * FROM table WHERE date BETWEEN
   ((TIMESTAMP WITH TIME ZONE 'epoch' + 1333065600 * INTERVAL '1 second') AT TIME ZONE 'UTC')
   AND
   ((TIMESTAMP WITH TIME ZONE 'epoch' + 1364601600 * INTERVAL '1 second') AT TIME ZONE 'UTC');

Please note that Javascript epoch accuracy is milliseconds, while Unix epoch is only seconds. You have to either discard last three digits from the input values (like I did in the example above), or divide by 1000 (example below).

SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 1333065600000/1000 * INTERVAL '1 second') AT TIME ZONE UTC;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM