'How to validate timestamp in javascript
How do you validate timestamp using javascript and timestamp to accept multiple formats e.g. YYYY-MM-DD HH:mm:ss.S, YYYY-MM-DD HH:mm:ss AM/PM.
Solution 1:[1]
You can validate if a string is a valid timestamp like this:
var valid = (new Date(timestamp)).getTime() > 0;
var valid = (new Date('2012-08-09')).getTime() > 0; // true
var valid = (new Date('abc')).getTime() > 0; // false
Revisiting this answer after many years, validating dates can still be challenging. However, some of the argument is that the built in parser accepts a number of input format, many of which have little relevance.
The question here is to validate a timestamp of multiple formats, and unless the date parser can help you, there is a need to convert the timestamp into a generic format that is comparable. How to convert into this format depends on the format of the input, and in case of incompatible inputs, a tailored conversion algorithm will have to be developed.
Either use the built in date parser, as described above, otherwise, you will have to parse the input manually, and validate it accordingly.
Solution 2:[2]
The solution of @Jørgen is nice but if you have a date before January 1, 1970
your timestamp will be a negative number but also a valid timestamp.
function isValidTimestamp(_timestamp) {
const newTimestamp = new Date(_timestamp).getTime();
return isNumeric(newTimestamp);
}
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
The numeric validation I took from the following SO answer.
For example:
isValidTimestamp('12/25/1965') // true
Solution 3:[3]
Every valid number is a timestamp. If it satisfies the condition of valid integer number then it will also satisfy the condition of the valid timestamp.
Timestamp = The number of milliseconds since 1970/01/01
Solution 4:[4]
var d = Date.parse(your_timestamp);
d
should be a valid number and not NaN.
Solution 5:[5]
You can't generically parse a date string without knowing beforehand what the format is, or at least that it is one of a limited number of formats.
If the date component is always in ISO8601 format (yyyy-mm-dd) and the time is either 24hr or 12hr with AM or PM, you should be able to easily split off the time, look for AM or PM, then treat the time as 12 or 24hr depending on whether it's present or not.
Timezones must be specified as either UTC (Z) or hours +/-UTC, abbreviations such as EST are ambiguous (and not standardised).
Solution 6:[6]
by using new Date().getTime();
you can do this
and doing something like this
var getDate="12-12-2012";
var myDate=getDate.split("-");
var getDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];
alert(new Date(getDate).getTime());
Solution 7:[7]
Warning! The answer from Jørgen doesn't work before 1970.
getTime
will return NaN
(Not a Number) when it's not a valid date so we'll just use the built-in function isNaN
to check, and inverse the value with !
.
!isNaN(new Date("1969-02-13").getTime())
Working with dates can be a hassle. It's recommended to use libraries that are battle-tested. Something like date-fns
or similar.
Solution 8:[8]
/**
* Determine whether string is timestamp
*
* @example
*
* isTimestamp('1606205966448'); // true
* isTimestamp(1606205966448); // true
* isTimestamp('1606205966448qwe'); // false
* isTimestamp('2020-11-24T08:19:26.448Z'); // false
*
* @param {string|number} n
* @returns {boolean}
*/
function isTimestamp(n) {
const parsed = parseFloat(n);
return !Number.isNaN(parsed) && Number.isFinite(parsed) && /^\d+\.?\d+$/.test(n);
}
export default isTimestamp;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Hristo Eftimov |
Solution 3 | radhe_shyam |
Solution 4 | David Hellsing |
Solution 5 | RobG |
Solution 6 | Gyan Chandra Srivastava |
Solution 7 | curly_brackets |
Solution 8 | Vladimir |