【发布时间】:2019-03-05 22:27:54
【问题描述】:
我有以下网址:
https://electric-stoat.glitch.me/api/timestamp/:date_string?2015-12-25
在我的 Express app.get 函数中,我想获取 2015-12-25 并返回如下所示的 JSON:
{unix: < The UNIX timestamp for 2015-12-15 in milliseconds >, utc: < The UTC timestamp for 2015-12-15 >}
如何从req.query 中获取2015-12-15 并将其转换为UNIX 时间戳和UTC 时间戳?这是我到目前为止所拥有的,忽略 if 语句的第一部分。在 else 我希望将 req.query 转换为 UNIX/UTC 时间戳。
app.get("/api/timestamp/:date_string?", function (req, res, date) {
// If query params are empty use the current date
if(Object.keys(req.query).length === 0) {
date = Date.now();
console.log("Date.now outputs : " + date);
var dateUTC = new Date().toUTCString();
res.json({
unix: date,
utc: dateUTC
});
}
// If query params are provided use date provided by req.query
else {
res.json({
unix: req.query,
utc: req.query
})
}
【问题讨论】:
标签: node.js express url timestamp unix-timestamp