Created
September 12, 2017 18:07
-
-
Save jorlee92/b100659e010f1771a6f3c248e1a8d8c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const PORT = 8080; | |
var express = require('express'); | |
var service = express(); | |
service.get("/", function(request, response){ | |
response.send("Usage: Load this same page with either a string representing the date, or a Unix timestamp at the end</br> you will be given both the timestamp and readable date as a response."); | |
}); | |
service.get("/:date", function(request, response){ | |
response.send(createTimestamps(request.params.date)); | |
// response.send("Hello") | |
}); | |
service.listen(PORT); | |
function createTimestamps(userInput){ | |
//Takes userInput and returns an object containing a unix timestamp and a readable date. | |
//Here we will determine if we can turn the userInput into an integer. | |
//Given that unix timestamps can be any length we only care if it is a number | |
var unixTimeStamp; | |
var humanTimeStamp; | |
if (!isNaN(Number.parseInt(userInput))){ | |
//Given that we have a number | |
unixTimeStamp = Number.parseInt(userInput); | |
} | |
else if (typeof(Date.parse(userInput) != typeof(Date.parse("Hello World")))){ | |
//If we are able to turn the user's input into a Date | |
//We cannot skip the first if statement because parsing a date from a number doesnt work | |
//Example Date.parse("715503600000") != 715503600000 | |
unixTimeStamp = Date.parse(userInput); | |
} | |
else { | |
//In the event we are unable to parse the user's input into a value | |
return { | |
"unix": null, | |
"human": null | |
} | |
} | |
var dateObject = new Date(unixTimeStamp); | |
var dateString = dateObject.toDateString(); | |
return { | |
"unix": unixTimeStamp, | |
"human": dateString | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment