Reputation: 2326
As I am almost completing the project from FCC (https://www.freecodecamp.com/challenges/timestamp-microservice)
I am not able to figure out why when the input is in standard time, it won't output its Unix timestamp correctly.
For instance, when I type:
http://localhost:3000/January%201%201970
it will output as so:
{"unix":"28800","natural":"January 1, 1970"}
It seems like there is an offset of 8 hours (28800 seconds, but even when I apply the utcOffset(), it doesn't change.
var express = require('express');
var path = require('path')
var app = express();
var moment = require('moment')
var port = 3000;
//homepage
app.get('/', function(req, res) {
var fileName = path.join(__dirname, 'index.html');
res.sendFile(fileName, function (err) {
if (err) {console.error(err)}
console.log('This is the homepage')
});
});
//input of the page
app.get('/:dataString', function(req, res) {
var dataString = req.params.dataString;
var output;
//Using regex, checks if the dataString has only number characters
if(/^[0-9]*$/.test(dataString)){
output = moment(dataString, "X")
} else{
console.log(dataString)
output = moment(dataString, "MMMM DD YYYY")
console.log(output.utc().format("X"))
}
if (output.isValid()){
res.json({
unix: output.utc().format("X"),
natural: output.utc().format("MMMM D, YYYY")
});
} else{
res.json({
unix: 'null',
natural: 'null'
});
}
})
app.listen(port,function(){
console.log("turn on. Port is: ", port)
})
Upvotes: 0
Views: 66
Reputation: 241475
In your code:
output = moment(dataString, "MMMM DD YYYY")
This is creating a moment in local time. The resulting timestamp reflects your local time zone's offset from UTC at the point in time of your dataString
.
If you wanted the input to be based on UTC, then it would be:
output = moment.utc(dataString, "MMMM DD YYYY")
Upvotes: 1