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.
-
Date.js? see http://stackoverflow.com/questions/2408202/jquery-datejs-is-there-validation-for-full-date – Alex K. Sep 14 '12 at 10:51
8 Answers
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.

- 8,820
- 9
- 47
- 67
-
1+1 "valid" as in what the Date constructor can parse using `Date.parse`. This is not necessarily consistent between browsers. – David Hellsing Sep 14 '12 at 10:56
-
2Thanks, however if the input format is not compliant with the parser, it will have to be converted in advance nevertheless. – Jørgen Sep 14 '12 at 11:01
-
3caution: This will return true with a date string of '4' which is technically a valid timestamp, but probably not a valid date in the context of your app. – Jason Nov 22 '13 at 19:20
-
1
-
It will return true for all positive integers which are not Dates. var valid = (new Date(3)).getTime() > 0; – Sudharshan Oct 11 '17 at 07:15
-
2When using this within an if statement I believe you can even remove > 0 and just use if ((new Date(timestamp)).getTime()) { console.log("It's a date!"); } – taykay08 Jan 18 '18 at 20:33
-
-
let hay = `The word 'freedom' is probably somewhere around byte 21008"` var valid = (new Date(hay)).getTime() > 0; console.log(valid); //return true – Cao Shouguang Jan 05 '22 at 07:50
-
Don't use this. It will only work for dates after 1970. This is when we started counting in Unix time, so dates before this will be in minus — and not work with this method. – curly_brackets Apr 21 '22 at 08:09
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

- 13,845
- 13
- 50
- 77
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

- 159
- 1
- 5
var d = Date.parse(your_timestamp);
d
should be a valid number and not NaN.

- 106,495
- 44
- 176
- 212

- 727
- 4
- 14
-
3-1 W3Schoola is awful, and [Date.parse](http://ecma-international.org/ecma-262/5.1/#sec-15.9.4.2) is implementation dependent so very unreliable. Date strings should be manually parsed. – RobG Sep 14 '12 at 11:14
-
1can you tell me why is w3schools awful and can you tell me a case why Date.parse is unreliable – Srinivas Sep 14 '12 at 11:43
-
[w3fools](http://w3fools.com/) has some hints. `Date.parse` was entirely implementation dependent in ECMA-262 ed 3, in ES5 browsers should support a modified ISO8601 long format (but some don't). `Date.parse('2012-02-01')` fails in IE < 9 and Safari, `Date.parse('2012/02/01')` doesn't but fails in most versions of Firefox. Some won't parse '01-02-2012' but will parse '01/02/2012', all treat them as peculiar US date format regardless of system settings. I've given up keeping track and just parse dates manually. – RobG Sep 14 '12 at 12:17
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).

- 142,382
- 31
- 172
- 209
/**
* 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;

- 195
- 3
- 5
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());

- 1,370
- 9
- 29
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.

- 5,491
- 15
- 58
- 102