How to send timestamp in Express js json response? How to send timestamp in Express js json response? express express

How to send timestamp in Express js json response?


You need to use Date.now()

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

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


This worked out perfectly to filter all Date

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


You can use moment.js (as suggested by @swapnesh). But I found it more suitable to simply save the data as a timestamp. Since I'm using mongoDB I'm using Number for the data type.

So my schema looks something like this:

var myDataSchema = {    start: {type: Number, required: "myDataSchema.start is a required field"},}

This will output:

{    "_id": "564c6828d4f028236cf1a6c8",    "start": ​1450969200}