Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send timestamp in Express js json response?

res.json({data: new Date()});

This is the response: {data: '2014-05-20T01:40:34.993Z'}

How to configure express to return timestamp instead of date string?

{data: 1343235454545}

like image 625
Junyu Wu Avatar asked Jul 03 '14 03:07

Junyu Wu


2 Answers

You need to use Date.now()

Example - res.json({data: Date.now()});

Then you will get result in timestamp "data": 1404359477253 likewise.

like image 149
swapnesh Avatar answered Oct 03 '22 07:10

swapnesh


This worked out perfectly to filter all Date

app.set('json replacer', function (key, value) {
  if (this[key] instanceof Date) {
// Your own custom date serialization
value = this[key].now();
  }
  return value;

});
like image 34
Ronald Coarite Avatar answered Oct 03 '22 07:10

Ronald Coarite