M
M
Michael2016-06-10 05:26:14
JavaScript
Michael, 2016-06-10 05:26:14

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)
});

When I pass as request unix timestamp. e.g.
http://localhost:8080/1450137600
I get the following object in response
{
    unix: 1450137600,
    natural: "December 15, 2015"
}

But if I send for example the same December 15, 2015 I will
http://localhost:8080/December 15, 2015
get
{
    unix: 1450098000,
    natural: "December 15, 2015"
}

And
http://localhost:8080/1450098000
{
    unix: 1450098000,
    natural: "December 15, 2015"
}

Why do I get different unix timestamps for the same input values? Moreover, both timestamps indicate the same date December 15, 2015

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2016-06-10
@id194695811

UTC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question