-3

I would have thought these two statements would have returned the same value:

Javascript (Chrome)

new Date(2018, 3, 1).getTime() /1000;
=> 1522533600

Postgres

SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2018-03-01 00:00:00 ');
=> 1519858800.0

As you can see there is a huge difference. What am I missing here?

Timezone information

Both queries are performed in the same timezone, confirmed as below.

Postgres

SELECT EXTRACT(TIMEZONE FROM now())/3600.0;
=> 2.0

Javascript

new Date().getTimezoneOffset();
-120

Solution

Thank you @yelliver and @Vao Tsun, the problem was that Javascript starts the month with zero. Confirming:

new Date(2018, 2, 1).getTime() /1000;
=> 1519858800
port5432
  • 5,889
  • 10
  • 60
  • 97
  • What's with the downvotes? – port5432 Apr 11 '18 at 07:05
  • Reading the documentation would have helped: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date (pay attention to the `month` parameter). – deceze Apr 11 '18 at 07:06
  • 1
    @ardochhigh adapt you post to have March against JS call please – Vao Tsun Apr 11 '18 at 07:09
  • Thank you @deceze, in fact I did read the documentation, and have been struggling with this problem for over an hour. I obviously missed the point that JS starts the month differently. I think SO is a fantastic tool, but it would benefit from less negative energy. Wouldn't it have been just as easy to state the answer? Have a nice day. – port5432 Apr 11 '18 at 07:10
  • 1
    @ardochhigh I have to admit that posts concerning javascript suffer from a lot of negative energy lately. – Matus Dubrava Apr 11 '18 at 07:13
  • 1
    @ardochhigh but it's still one hour diff according to code you pasted - are you saying they match?.. – Vao Tsun Apr 11 '18 at 07:13
  • yes they match, see the posted solution in the original post. – port5432 Apr 11 '18 at 07:17
  • t=# SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2018-03-01 00:00:00+01:00'); date_part ------------ 1519858800 (1 row) so I still don't understand why you have time zone `+2` and get SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2018-03-01 00:00:00 '); => 1519858800.0 – Vao Tsun Apr 11 '18 at 07:19
  • That's the timezone offset of the server ... I wanted to help any posters by confirming there was no difference according to timezone. Both the Javascript and SQL commands were run under the same offset. – port5432 Apr 11 '18 at 07:20

1 Answers1

2
new Date(2018, 3, 1)  = Sun Apr 01 2018

It's April, not March

yelliver
  • 5,648
  • 5
  • 34
  • 65