I’m having some confusion with this project. I can enter both a unix timestamp and a natural date on their own and get a result. I’m trying to write an if statement to determine what the input is and then give the appropriate response. I’m using moment as well.
Right now I have:
//unix
if (typeof parseInt(input) === "number") {
return {unix: input, natural: moment.unix(input).format("MMMM D, YYYY")};
}
//natural
else if (typeof input !== "number") {
return {unix: moment(input).unix(), natural: moment(input, "M D YYYY").format("MMMM D, YYYY")};
}
//invalid
else {
return {unix: null, natural: null};
}
The problem is that my input variable comes as a string. When I know the input is supposed to be a number I used parseInt
to convert it. But if you were to input the date as 01-01-1980
It will pick that as a unix value and not as a natural date as intended.