8

I get three variables through a user input, that contain the year of a date, the month and the day. I've already checked if the month var is between 1–12 and so on.

Now I want to check if it's a real date and not a date that doesn't exist like 31–06–2011.

My first idea was to make a new Date instance:

var year = 2011;
var month = 5; // five because the months start with 0 in JavaScript - June
var day = 31;
var myDate = new Date(2011,5,31);
console.log(myDate);

But myDate doesn't return false, because it's not a valid date. Instead it returns 'Fri Jul 01 2011 [...]'.

Any ideas how I can check for an invalid date?

2
  • Welcome to Stack Overflow, dotweb! Please don't put tags in the title, thanks! Commented Jul 23, 2011 at 12:13
  • Tags belong in the tags, not in the title. Commented Jul 23, 2011 at 13:46

5 Answers 5

13

Try this:

if ((myDate.getMonth()+1!=month)||(myDate.getDate()!=day)||(myDate.getFullYear()!=year))
    alert("Date Invalid.");
Sign up to request clarification or add additional context in comments.

1 Comment

You only need to check two components, but all three doesn't hurt. :-)
5
if ((myDate.getDate() != day) || 
    (myDate.getMonth() != month - 1) || 
    (myDate.getFullYear() != year))
{
    return false;
}

JavaScript just converts entered in Date constructor month, year, day, etc.. in simple int value (milliseconds) and then formats it to represent in string format. You can create new Date(2011, 100, 100) and everythig will ok :)

Comments

1

You could possibly do what you do now and construct a new Date object and then afterwards check the value of myDate.getFullYear(), myDate.getMonth(), myDate.getDate(), to ensure that those values match the input values. Keep in mind that getMonth() and getDate() are 0 indexed, so January is month 0 and December is month 11.

Here's an example:

function isValidDate(year, month, day) {
    var d = new Date(year, month, day);

    return d.getFullYear() === year && d.getMonth() === month && d.getDate() === day;
}

console.log(isValidDate(2011,5,31));
console.log(isValidDate(2011,5,30));

Comments

0

This is a old question question however to help me and other after me, here is a php checkdate solution from the following webpage:

https://locutus.io/php/datetime/checkdate/

function checkdate (m, d, y) {
    return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0))
    .getDate()
}

Comments

-1

I think this the right way to do it:

function isValidDate(year, month, day) {
  var d = new Date(year, month, day)
  if (month == 12) {
    year = parseInt(year) * 1 + 1 * 1
    month = 0
  }
  day = parseInt(day)
  month = parseInt(month)
  year = parseInt(year)
  if (month === 2 && day > 29) {
    return false
  }
  return (
    d.getFullYear() === year && d.getMonth() === month && d.getDate() === day
  )
}

1 Comment

Ehm, this was already answered over half a year ago! Thanks anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.