Last active
September 13, 2017 17:24
-
-
Save jorlee92/2044c3fe667d31e7a78de9d57d082a61 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)); | |
}); | |
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 (!isNaN(Date.parse(userInput))){ | |
//If we are able to turn the user's input into a Date, if you give it an invalid input it will return NaN | |
//We cannot skip the first if statement because parsing a date from a number doesnt work | |
//Example Date.parse("715503600000") != 715503600000 | |
//isNaN(Date.parse("Hello!")) = true | |
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 | |
} | |
} | |
//Create a new object from the unix timestamp, and a new string from that object. | |
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