how to format Date in json response?
now, detault like this: createTime: "2015-11-03T09:05:33.026Z"
Hi! So assuming you are using res.json (http://expressjs.com/4x/api.html#res.json), then it's important to note that all we do is the standard JSON.stringify operation (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) on the argument, and the second argument for that you can define as the json replacer setting (http://expressjs.com/4x/api.html#app.set). So, if you wanted to stringify Date objects differently, for example, here is a way you can define this on your app:
app.set('json replacer', function (key, value) {
if (this[key] instanceof Date) {
// Your own custom date serialization
value = this[key].toLocaleString();
}
return value;
});
3Q
Most helpful comment
Hi! So assuming you are using
res.json(http://expressjs.com/4x/api.html#res.json), then it's important to note that all we do is the standardJSON.stringifyoperation (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) on the argument, and the second argument for that you can define as thejson replacersetting (http://expressjs.com/4x/api.html#app.set). So, if you wanted to stringifyDateobjects differently, for example, here is a way you can define this on your app: