If it's a unix timestamp, it's numbers only, and if it's not, it's an actual string (not empty or boolean) and javascript has a function for that, isNaN
isNaN(datestring_or_number)
can be easily used
new Date(isNaN(str) ? str : NumberLong(str));