Detecting an "invalid date" Date instance in JavaScript
JavascriptDateJavascript Problem Overview
I'd like to tell the difference between valid and invalid date objects in JS, but couldn't figure out how:
var d = new Date("foo");
console.log(d.toString()); // shows 'Invalid Date'
console.log(typeof d); // shows 'object'
console.log(d instanceof Date); // shows 'true'
Any ideas for writing an isValidDate
function?
- Ash recommended
Date.parse
for parsing date strings, which gives an authoritative way to check if the date string is valid. - What I would prefer, if possible, is have my API accept a Date instance and to be able to check/assert whether it's valid or not. Borgar's solution does that, but I need to test it across browsers. I also wonder whether there's a more elegant way.
- Ash made me consider not having my API accept
Date
instances at all, this would be easiest to validate. - Borgar suggested testing for a
Date
instance, and then testing for theDate
's time value. If the date is invalid, the time value isNaN
. I checked with http://www.ecma-international.org/ecma-262/6.0/index.html#sec-time-values-and-time-range">ECMA-262</a> and this behavior is in the standard, which is exactly what I'm looking for.
Javascript Solutions
Solution 1 - Javascript
Here's how I would do it:
if (Object.prototype.toString.call(d) === "[object Date]") {
// it is a date
if (isNaN(d)) { // d.getTime() or d.valueOf() will also work
// date object is not valid
} else {
// date object is valid
}
} else {
// not a date object
}
Update [2018-05-31]: If you are not concerned with Date objects from other JS contexts (external windows, frames, or iframes), this simpler form may be preferred:
function isValidDate(d) {
return d instanceof Date && !isNaN(d);
}
Update [2021-02-01]: Please note that there is a fundamental difference between "invalid dates" (2013-13-32
) and "invalid date objects" (new Date('foo')
). This answer does not deal with validating date input, only if a Date instance is valid.
Solution 2 - Javascript
Instead of using new Date()
you should use:
var timestamp = Date.parse('foo');
if (isNaN(timestamp) == false) {
var d = new Date(timestamp);
}
Date.parse()
returns a timestamp, an integer representing the number of milliseconds since 01/Jan/1970. It will return NaN
if it cannot parse the supplied date string.
Solution 3 - Javascript
You can check the validity of a Date
object d
via
d instanceof Date && isFinite(d)
To avoid cross-frame issues, one could replace the instanceof
check with
Object.prototype.toString.call(d) === '[object Date]'
A call to getTime()
as in Borgar's answer is unnecessary as isNaN()
and isFinite()
both implicitly convert to number.
Solution 4 - Javascript
My solution is for simply checking whether you get a valid date object:
Implementation
Date.prototype.isValid = function () {
// An invalid date object returns NaN for getTime() and NaN is the only
// object not strictly equal to itself.
return this.getTime() === this.getTime();
};
Usage
var d = new Date("lol");
console.log(d.isValid()); // false
d = new Date("2012/09/11");
console.log(d.isValid()); // true
Solution 5 - Javascript
shortest answer to check valid date
if(!isNaN(date.getTime()))
Solution 6 - Javascript
moment.js
You can simply useHere is an example:
var m = moment('2015-11-32', 'YYYY-MM-DD');
m.isValid(); // false
The validation section in the documentation is quite clear.
And also, the following parsing flags result in an invalid date:
overflow
: An overflow of a date field, such as a 13th month, a 32nd day of the month (or a 29th of February on non-leap years), a 367th day of the year, etc. overflow contains the index of the invalid unit to match #invalidAt (see below); -1 means no overflow.invalidMonth
: An invalid month name, such as moment('Marbruary', 'MMMM');. Contains the invalid month string itself, or else null.empty
: An input string that contains nothing parsable, such as moment('this is nonsense');. Boolean.- Etc.
Source: http://momentjs.com/docs/
Solution 7 - Javascript
I have seen some answers that came real close to this little snippet.
JavaScript way:
function isValidDate(dateObject){
return new Date(dateObject).toString() !== 'Invalid Date';
}
console.log(isValidDate('WTH')); // -> false
console.log(isValidDate(new Date('WTH'))); // -> false
console.log(isValidDate(new Date())); // -> true
ES2015 way:
const isValidDate = dateObject => new Date(dateObject)
.toString() !== 'Invalid Date';
console.log(isValidDate('WTH')); // -> false
console.log(isValidDate(new Date('WTH'))); // -> false
console.log(isValidDate(new Date())); // -> true
Solution 8 - Javascript
Would like to mention that the jQuery UI DatePicker widget has a very good date validator utility method that checks for format and validity (e.g., no 01/33/2013 dates allowed).
Even if you don't want to use the datepicker widget on your page as a UI element, you can always add its .js library to your page and then call the validator method, passing the value you want to validate into it. To make life even easier, it takes a string as input, not a JavaScript Date object.
See: http://api.jqueryui.com/datepicker/
It's not listed as a method, but it is there-- as a utility function. Search the page for "parsedate" and you'll find:
$.datepicker.parseDate( format, value, settings ) - Extract a date from a string value with a specified format.
Example usage:
var stringval = '01/03/2012';
var testdate;
try {
testdate = $.datepicker.parseDate('mm/dd/yy', stringval);
// Notice 'yy' indicates a 4-digit year value
} catch (e)
{
alert(stringval + ' is not valid. Format must be MM/DD/YYYY ' +
'and the date value must be valid for the calendar.';
}
(More info re specifying date formats is found at http://api.jqueryui.com/datepicker/#utility-parseDate)
In the above example, you wouldn't see the alert message since '01/03/2012' is a calendar-valid date in the specified format. However if you made 'stringval' equal to '13/04/2013', for example, you would get the alert message, since the value '13/04/2013' is not calendar-valid.
If a passed-in string value is successfully parsed, the value of 'testdate' would be a Javascript Date object representing the passed-in string value. If not, it'd be undefined.
Solution 9 - Javascript
// check whether date is valid
var t = new Date('2011-07-07T11:20:00.000+00:00x');
valid = !isNaN(t.valueOf());
Solution 10 - Javascript
I really liked Christoph's approach (but didn't have enough of a reputation to vote it up). For my use, I know I will always have a Date object so I just extended date with a valid() method.
Date.prototype.valid = function() {
return isFinite(this);
}
Now I can just write this and it's much more descriptive than just checking isFinite in code...
d = new Date(userDate);
if (d.valid()) { /* do stuff */ }
Solution 11 - Javascript
I use the following code to validate values for year, month and date.
function createDate(year, month, _date) {
var d = new Date(year, month, _date);
if (d.getFullYear() != year
|| d.getMonth() != month
|| d.getDate() != _date) {
throw "invalid date";
}
return d;
}
For details, refer to Check date in javascript
Solution 12 - Javascript
you can check the valid format of txDate.value with this scirpt. if it was in incorrect format the Date obejct not instanced and return null to dt .
var dt = new Date(txtDate.value)
if (isNaN(dt))
And as @MiF's suggested in short way
if(isNaN(new Date(...)))
Solution 13 - Javascript
Too many complicated answers here already, but a simple line is sufficient (ES5):
Date.prototype.isValid = function (d) { return !isNaN(Date.parse(d)) } ;
or even in ES6 :
Date.prototype.isValid = d => !isNaN(Date.parse(d));
Solution 14 - Javascript
After reading every answer so far, I am going to offer the most simple of answers.
Every solution here mentions calling date.getTime()
. However, this is not needed, as the default conversion from Date to Number is to use the getTime() value. Yep, your type checking will complain. :) And the OP cleary knows they have a Date
object, so no need to test for that either.
To test for an invalid date:
isNaN(date)
To test for a valid date:
!isNaN(date)
Solution 15 - Javascript
Why am I writing a 48th answer after so many have tried before me? Most of the answers are partly correct and will not work in every situation, while others are unnecessarily verbose and complex. Below is a very concise solution. This will checking if it is Date
type and then check if a valid date object:
return x instanceof Date && !!x.getDate();
Now for parsing date Text: Most of the solutions use Date.parse(), or "new Date()" -- both of these will fail certain situations and can be dangerous. JavaScript parses a wide variety of formats and also is dependent on localization. For example, strings like "1" and "blah-123" will parse as a valid date.
Then there are posts that either use a ton of code, or a mile-long RegEx, or use third party frameworks.
This is dead simple method to validate a date string.
function isDate(txt) {
var matches = txt.match(/^\d?\d\/(\d?\d)\/\d{4}$/); //Note: "Day" in the RegEx is parenthesized
return !!matches && !!Date.parse(txt) && new Date(txt).getDate()==matches[1];
}
TEST THE FUNCTION
<br /><br />
<input id="dt" value = "12/21/2020">
<input type="button" value="validate" id="btnAction" onclick="document.getElementById('rslt').innerText = isDate(document.getElementById('dt').value)">
<br /><br />
Result: <span id="rslt"></span>
The first line of isDate parses the input text with a simple RegEx to validate for date formats mm/dd/yyyy, or m/d/yyyy. For other formats, you will need to change the RegEx accordingly, e.g. for dd-mm-yyyy the RegEx becomes /^(\d?\d)-\d?\d-\d{4}$/
If parse fails, "matches" is null, otherwise it stores the day-of-month. The second lines does more tests to ensure it is valid date and eliminates cases like 9/31/2021 (which JavaScript permits). Finally note the double-whack (!!) converts "falsy" to a boolean false.
Solution 16 - Javascript
This just worked for me
new Date('foo') == 'Invalid Date'; //is true
However this didn't work
new Date('foo') === 'Invalid Date'; //is false
Solution 17 - Javascript
I wrote the following solution based on Borgar's solution. Included in my library of auxiliary functions, now it looks like this:
Object.isDate = function(obj) {
/// <summary>
/// Determines if the passed object is an instance of Date.
/// </summary>
/// <param name="obj">The object to test.</param>
return Object.prototype.toString.call(obj) === '[object Date]';
}
Object.isValidDate = function(obj) {
/// <summary>
/// Determines if the passed object is a Date object, containing an actual date.
/// </summary>
/// <param name="obj">The object to test.</param>
return Object.isDate(obj) && !isNaN(obj.getTime());
}
Solution 18 - Javascript
For Angular.js projects you can use:
angular.isDate(myDate);
Solution 19 - Javascript
None of these answers worked for me (tested in Safari 6.0) when trying to validate a date such as 2/31/2012, however, they work fine when trying any date greater than 31.
So I had to brute force a little. Assuming the date is in the format mm/dd/yyyy
. I am using @broox answer:
Date.prototype.valid = function() {
return isFinite(this);
}
function validStringDate(value){
var d = new Date(value);
return d.valid() && value.split('/')[0] == (d.getMonth()+1);
}
validStringDate("2/29/2012"); // true (leap year)
validStringDate("2/29/2013"); // false
validStringDate("2/30/2012"); // false
Solution 20 - Javascript
Date.prototype.toISOString
throws RangeError
(at least in Chromium and Firefox) on invalid dates. You can use it as a means of validation and may not need isValidDate
as such (EAFP). Otherwise it's:
function isValidDate(d)
{
try
{
d.toISOString();
return true;
}
catch(ex)
{
return false;
}
}
Solution 21 - Javascript
I rarely recommend libraries when one can do without. But considering the plethora of answers so far it seems worth pointing out that the popular library "date-fns" has a function isValid
. The following documentation is taken from their website:
isValid argument | Before v2.0.0 | v2.0.0 onward |
---|---|---|
new Date() | true | true |
new Date('2016-01-01') | true | true |
new Date('') | false | false |
new Date(1488370835081) | true | true |
new Date(NaN) | false | false |
'2016-01-01' | TypeError | false |
'' | TypeError | false |
1488370835081 | TypeError | true |
NaN | TypeError | false |
Solution 22 - Javascript
Inspired by Borgar's approach I made sure that the code not only validates the date, but actually makes sure the date is a real date, meaning that dates like 31/09/2011 and 29/02/2011 are not allowed.
function(dateStr) {
s = dateStr.split('/');
d = new Date(+s[2], s[1] - 1, +s[0]);
if (Object.prototype.toString.call(d) === "[object Date]") {
if (!isNaN(d.getTime()) && d.getDate() == s[0] &&
d.getMonth() == (s[1] - 1)) {
return true;
}
}
return "Invalid date!";
}
Solution 23 - Javascript
None of the above solutions worked for me what did work however is
function validDate (d) {
var date = new Date(d);
var day = "" + date.getDate();
if ( day.length == 1 ) day = "0" + day;
var month = "" + (date.getMonth() + 1);
if ( month.length == 1 ) month = "0" + month;
var year = "" + date.getFullYear();
return (( month + "/" + day + "/" + year ) == d );
}
the code above will see when JS makes 02/31/2012 into 03/02/2012 that it's not valid
Solution 24 - Javascript
IsValidDate: function(date) {
var regex = /\d{1,2}\/\d{1,2}\/\d{4}/;
if (!regex.test(date)) return false;
var day = Number(date.split("/")[1]);
date = new Date(date);
if (date && date.getDate() != day) return false;
return true;
}
Solution 25 - Javascript
I've written this function. Pass it a string parameter and it will determine whether it's a valid date or not based on this format "dd/MM/yyyy".
here is a test
input: "hahaha",output: false.
input: "29/2/2000",output: true.
input: "29/2/2001",output: false.
function isValidDate(str) {
var parts = str.split('/');
if (parts.length < 3)
return false;
else {
var day = parseInt(parts[0]);
var month = parseInt(parts[1]);
var year = parseInt(parts[2]);
if (isNaN(day) || isNaN(month) || isNaN(year)) {
return false;
}
if (day < 1 || year < 1)
return false;
if(month>12||month<1)
return false;
if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day > 31)
return false;
if ((month == 4 || month == 6 || month == 9 || month == 11 ) && day > 30)
return false;
if (month == 2) {
if (((year % 4) == 0 && (year % 100) != 0) || ((year % 400) == 0 && (year % 100) == 0)) {
if (day > 29)
return false;
} else {
if (day > 28)
return false;
}
}
return true;
}
}
Solution 26 - Javascript
I combined the best performance results I found around that check if a given object:
- is a Date instance (benchmark here)
- has a valid date (benchmark here)
The result is the following:
function isValidDate(input) {
if(!(input && input.getTimezoneOffset && input.setUTCFullYear))
return false;
var time = input.getTime();
return time === time;
};
Solution 27 - Javascript
A ready function based on top rated answer:
/**
* Check if date exists and is valid.
*
* @param {String} dateString Date in YYYY-mm-dd format.
*/
function isValidDate(dateString) {
var isValid = false;
var date;
date =
new Date(
dateString);
if (
Object.prototype.toString.call(
date) === "[object Date]") {
if (isNaN(date.getTime())) {
// Date is unreal.
} else {
// Date is real if month and day match each other in date and string (otherwise may be shifted):
isValid =
date.getUTCMonth() + 1 === dateString.split("-")[1] * 1 &&
date.getUTCDate() === dateString.split("-")[2] * 1;
}
} else {
// It's not a date.
}
return isValid;
}
Solution 28 - Javascript
No one has mentioned it yet, so Symbols would also be a way to go:
Symbol.for(new Date("Peter")) === Symbol.for("Invalid Date") // true
Symbol.for(new Date()) === Symbol.for("Invalid Date") // false
console.log('Symbol.for(new Date("Peter")) === Symbol.for("Invalid Date")', Symbol.for(new Date("Peter")) === Symbol.for("Invalid Date")) // true
console.log('Symbol.for(new Date()) === Symbol.for("Invalid Date")', Symbol.for(new Date()) === Symbol.for("Invalid Date")) // false
Be aware of: https://caniuse.com/#search=Symbol
Solution 29 - Javascript
Date object to string is more simple and reliable way to detect if both fields are valid date. e.g. If you enter this "-------" to the date input field. Some of the above answers won't work.
jQuery.validator.addMethod("greaterThan",
function(value, element, params) {
var startDate = new Date($(params).val());
var endDate = new Date(value);
if(startDate.toString() === 'Invalid Date' || endDate.toString() === 'Invalid Date') {
return false;
} else {
return endDate > startDate;
}
},'Must be greater than {0}.');
Solution 30 - Javascript
For int 1-based components of a date:
var is_valid_date = function(year, month, day) {
var d = new Date(year, month - 1, day);
return d.getFullYear() === year && (d.getMonth() + 1) === month && d.getDate() === day
};
Tests:
is_valid_date(2013, 02, 28)
&& is_valid_date(2016, 02, 29)
&& !is_valid_date(2013, 02, 29)
&& !is_valid_date(0000, 00, 00)
&& !is_valid_date(2013, 14, 01)
Solution 31 - Javascript
you can convert your date and time to milliseconds getTime()
this getTime()
Method return Not a Number NaN
when not valid
if(!isNaN(new Date("2012/25/255").getTime()))
return 'valid date time';
return 'Not a valid date time';
Solution 32 - Javascript
The selected answer is excellent, and I'm using it as well. However, if you're looking for a way to validate user date input, you should be aware that the Date object is very persistent about making what might appear to be invalid construction arguments into valid ones. The following unit test code illustrates the point:
QUnit.test( "valid date test", function( assert ) {
//The following are counter-examples showing how the Date object will
//wrangle several 'bad' dates into a valid date anyway
assert.equal(isValidDate(new Date(1980, 12, 15)), true);
d = new Date();
d.setFullYear(1980);
d.setMonth(1);
d.setDate(33);
assert.equal(isValidDate(d), true);
assert.equal(isValidDate(new Date(1980, 100, 150)), true);
//If you go to this exterme, then the checker will fail
assert.equal(isValidDate(new Date("This is junk")), false);
//This is a valid date string
assert.equal(isValidDate(new Date("November 17, 1989")), true);
//but is this?
assert.equal(isValidDate(new Date("November 35, 1989")), false);
//Ha! It's not. So, the secret to working with this version of
//isValidDate is to pass in dates as text strings... Hooboy
//alert(d.toString());
});
Solution 33 - Javascript
function isValidDate(strDate) {
var myDateStr= new Date(strDate);
if( ! isNaN ( myDateStr.getMonth() ) ) {
return true;
}
return false;
}
Call it like this
isValidDate(""2015/5/2""); // => true
isValidDate(""2015/5/2a""); // => false
Solution 34 - Javascript
This flavor of isValidDate uses a regular expression that handles leap years. It works on regular dates, but not iso ones:
function isValidDate(value) {
return /((^(10|12|0?[13578])([/])(3[01]|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(11|0?[469])([/])(30|[12][0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(2[0-8]|1[0-9]|0?[1-9])([/])((1[8-9]\d{2})|([2-9]\d{3}))$)|(^(0?2)([/])(29)([/])([2468][048]00)$)|(^(0?2)([/])(29)([/])([3579][26]00)$)|(^(0?2)([/])(29)([/])([1][89][0][48])$)|(^(0?2)([/])(29)([/])([2-9][0-9][0][48])$)|(^(0?2)([/])(29)([/])([1][89][2468][048])$)|(^(0?2)([/])(29)([/])([2-9][0-9][2468][048])$)|(^(0?2)([/])(29)([/])([1][89][13579][26])$)|(^(0?2)([/])(29)([/])([2-9][0-9][13579][26])$))/.test(value)
}
function test(value) {
console.log(`${value} valid: ${isValidDate(value)}`)
}
<button onClick="test('foo')">foo</button>
<button onClick="test('2/20/2000')">2/20/2000</button>
<button onClick="test('20/2/2000')">20/2/2000</button>
<button onClick="test('2022-02-02T18:51:53.517Z')">2022-02-02T18:51:53.517Z</button>
Solution 35 - Javascript
Simple and elegant solution:
const date = new Date(`${year}-${month}-${day} 00:00`)
const isValidDate = (Boolean(+date) && date.getDate() == day)
sources:
[1] https://medium.com/@esganzerla/simple-date-validation-with-javascript-caea0f71883c
[2] https://stackoverflow.com/questions/39223481/incorrect-date-shown-in-new-date-in-javascript
Solution 36 - Javascript
I think some of this is a long process. We can cut it short as shown below:
function isValidDate(dateString) {
debugger;
var dateStringSplit;
var formatDate;
if (dateString.length >= 8 && dateString.length<=10) {
try {
dateStringSplit = dateString.split('/');
var date = new Date();
date.setYear(parseInt(dateStringSplit[2]), 10);
date.setMonth(parseInt(dateStringSplit[0], 10) - 1);
date.setDate(parseInt(dateStringSplit[1], 10));
if (date.getYear() == parseInt(dateStringSplit[2],10) && date.getMonth()+1 == parseInt(dateStringSplit[0],10) && date.getDate() == parseInt(dateStringSplit[1],10)) {
return true;
}
else {
return false;
}
} catch (e) {
return false;
}
}
return false;
}
Solution 37 - Javascript
Generally I'd stick with whatever Date implantation is in the browser stack. Which means you will always get "Invalid Date" when calling toDateString() in Chrome, Firefox, and Safari as of this reply's date.
if(!Date.prototype.isValidDate){
Date.prototype.isValidDate = function(){
return this.toDateString().toLowerCase().lastIndexOf('invalid') == -1;
};
}
I did not test this in IE though.
Solution 38 - Javascript
function isValidDate(date) {
return !! (Object.prototype.toString.call(date) === "[object Date]" && +date);
}
Solution 39 - Javascript
So I liked @Ask Clarke answer with little improvement by adding try catch block for dates which cannot go through var d = new Date(d) -
function checkIfDateNotValid(d) {
try{
var d = new Date(d);
return !(d.getTime() === d.getTime()); //NAN is the only type which is not equal to itself.
}catch (e){
return true;
}
}
Solution 40 - Javascript
I have a solution.
const isInvalidDate = (dateString) => JSON.stringify(new Date(dateString)) === 'null';
const invalidDate = new Date('Hello');
console.log(isInvalidDate(invalidDate)); //true
const validDate = new Date('2021/02/08');
console.log(isInvalidDate(validDate)); //false
Solution 41 - Javascript
var isDate_ = function(input) {
var status = false;
if (!input || input.length <= 0) {
status = false;
} else {
var result = new Date(input);
if (result == 'Invalid Date') {
status = false;
} else {
status = true;
}
}
return status;
}
Solution 42 - Javascript
This function validates a string date in digit formats delimited by a character, e.g. dd/mm/yyyy, mm/dd/yyyy
/*
Param :
1)the date in string data type
2)[optional - string - default is "/"] the date delimiter, most likely "/" or "-"
3)[optional - int - default is 0] the position of the day component when the date string is broken up via the String.split function (into arrays)
4)[optional - int - default is 1] the position of the month component when the date string is broken up via the String.split function (into arrays)
5)[optional - int - default is 2] the position of the year component when the date string is broken up via the String.split function (into arrays)
Return : a javascript date is returned if the params are OK else null
*/
function IsValidDate(strDate, strDelimiter, iDayPosInArray, iMonthPosInArray, iYearPosInArray) {
var strDateArr; //a string array to hold constituents day, month, and year components
var dtDate; //our internal converted date
var iDay, iMonth, iYear;
//sanity check
//no integer checks are performed on day, month, and year tokens as parsing them below will result in NaN if they're invalid
if (null == strDate || typeof strDate != "string")
return null;
//defaults
strDelimiter = strDelimiter || "/";
iDayPosInArray = undefined == iDayPosInArray ? 0 : iDayPosInArray;
iMonthPosInArray = undefined == iMonthPosInArray ? 1 : iMonthPosInArray;
iYearPosInArray = undefined == iYearPosInArray ? 2 : iYearPosInArray;
strDateArr = strDate.split(strDelimiter);
iDay = parseInt(strDateArr[iDayPosInArray],10);
iMonth = parseInt(strDateArr[iMonthPosInArray],10) - 1; // Note: months are 0-based
iYear = parseInt(strDateArr[iYearPosInArray],10);
dtDate = new Date(
iYear,
iMonth, // Note: months are 0-based
iDay);
return (!isNaN(dtDate) && dtDate.getFullYear() == iYear && dtDate.getMonth() == iMonth && dtDate.getDate() == iDay) ? dtDate : null; // Note: months are 0-based
}
Example call:
var strDate="18-01-1971";
if (null == IsValidDate(strDate)) {
alert("invalid date");
}
Solution 43 - Javascript
Date.valid = function(str){
var d = new Date(str);
return (Object.prototype.toString.call(d) === "[object Date]" && !isNaN(d.getTime()));
}
https://gist.github.com/dustinpoissant/b83750d8671f10c414b346b16e290ecf
Solution 44 - Javascript
Yet another way to check whether the date is a valid date object:
const isValidDate = (date) =>
typeof date === 'object' &&
typeof date.getTime === 'function' &&
!isNaN(date.getTime())
Solution 45 - Javascript
Try something like this:
if (!('null' === JSON.stringify(new Date('wrong date')))) console.log('correct');
else console.log('wrong');
Solution 46 - Javascript
If you use io-ts, you can use the decoder DateFromISOString directly.
import { DateFromISOString } from 'io-ts-types/lib/DateFromISOString'
const decoded = DateFromISOString.decode('2020-05-13T09:10:50.957Z')
Solution 47 - Javascript
Pure JavaScript solution:
const date = new Date(year, (+month-1), day);
const isValidDate = (Boolean(+date) && date.getDate() == day);
Also works on leap years!
Credit to https://medium.com/@esganzerla/simple-date-validation-with-javascript-caea0f71883c
Solution 48 - Javascript
Why i Suggest moment.js
it is very popular library
simple to solve all date and time,format,timezone problems
easy to check string date valid or not
var date = moment("2016-10-19");
date.isValid()
we can't solve simple way to validate all the cases
Disspointment
if i insert in valid number like 89,90,95 in new Date() above few answare , i am getting bad result however it return true
const isValidDate = date => {
console.log('input'+date)
var date=new Date(date);
console.log(date)
return !! (Object.prototype.toString.call(date) === "[object Date]" && +date)
//return !isNaN(date.getTime())
}
var test="05/04/2012"
console.log(isValidDate(test))
var test="95"
console.log(isValidDate(test))
var test="89"
console.log(isValidDate(test))
var test="80"
console.log(isValidDate(test))
var test="badstring"
console.log(isValidDate(test))
Solution 49 - Javascript
date.parse(valueToBeTested) > 0
is all that's needed. A valid date will return the epoch value and an invalid value will return NaN which will fail > 0
test by virtue of not even being a number.
This is so simple that a helper function won't save code though it might be a bit more readable. If you wanted one:
String.prototype.isDate = function() {
return !Number.isNaN(Date.parse(this));
}
OR
To use:
"StringToTest".isDate();