Answer the question
In order to leave comments, you need to log in
Moment.js Why do I get different Unix Timestamp values with "same" input values?
I have a simple code that implements the Timestamp API. It takes a date as input as a string MMMM-DD-YYYY or as a Unix Timestamp, converts and returns MMMM-DD-YYYY or Unix Timestamp respectively
var express = require('express');
var moment = require('moment');
var url = require('url');
var cors = require('cors');
var port = process.env.PORT || 8080;
var app = express();
app.use(cors());
app.route('/').get(function (req, res) {
res.sendFile(process.cwd() + '/index.html')
});
app.get('/:date', function(req, res) {
var URL = url.parse(req.url).pathname.replace(/%20/g, ' ').slice(1);
if (isNaN(URL)) {
var unixtime = moment(URL, "LL").unix()
var naturaltime = moment(URL, "LL").format("LL")
} else {
var unixtime = moment.unix(URL, "LL").unix()
var naturaltime = moment.unix(URL).format("LL")
}
res.send(JSON.stringify({
unix : unixtime,
natural : naturaltime
}))
});
app.listen(port, function() {
console.log('Node.js listening on port ' + port)
});
http://localhost:8080/1450137600
{
unix: 1450137600,
natural: "December 15, 2015"
}
http://localhost:8080/December 15, 2015
{
unix: 1450098000,
natural: "December 15, 2015"
}
http://localhost:8080/1450098000
{
unix: 1450098000,
natural: "December 15, 2015"
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question