Ad

Unexpected JavaScript Date Result

- 1 answer

I have the following code to format dates:

var currentDate = dateFormat(now, "yyyy-mm-dd"); //dateFormat is a npm package
console.log(currentDate) //returns 2015-12-29

My currentDate value is saved to a PostgreSQL database. HOWEVER, when I view it, it is 2015-12-29T05:00:00.000Z. Does anyone know why that additional string is there and how I can get rid of it?

Ad

Answer

This is because the column in Postgres table is of type timestamp (with or without time zone). If you can change it to date you will see expected format. However, you can always cast the value to type date:

select '2015-12-29T05:00:00.000Z'::timestamp;
      timestamp      
---------------------
 2015-12-29 05:00:00
(1 row)

select '2015-12-29T05:00:00.000Z'::date;
    date    
------------
 2015-12-29
(1 row)
Ad
source: stackoverflow.com
Ad