admin管理员组

文章数量:1196423

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?

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?

Share Improve this question edited Mar 3, 2015 at 0:14 PObdr 1399 bronze badges asked Jul 23, 2011 at 12:12 js-coderjs-coder 8,33610 gold badges43 silver badges59 bronze badges 2
  • Welcome to Stack Overflow, dotweb! Please don't put tags in the title, thanks! – user142019 Commented Jul 23, 2011 at 12:13
  • Tags belong in the tags, not in the title. – user142019 Commented Jul 23, 2011 at 13:46
Add a comment  | 

5 Answers 5

Reset to default 13

Try this:

if ((myDate.getMonth()+1!=month)||(myDate.getDate()!=day)||(myDate.getFullYear()!=year))
    alert("Date Invalid.");
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 :)

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));

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()
}

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
  )
}

本文标签: Check date in JavaScriptStack Overflow