Comprobar fecha 2


/ Published in: JavaScript
Save to your folder(s)

isDate(y: Integer, m: Integer, d: Integer): Integer

Checks a date and returns 0 if it's valid or one of the error codes bellow.

y year
m month
d day

Return codes
* 0 = Valid date
* 1 = Date format invalid (regular expression failed or amount of arguments != 3)
* 2 = Day isn't between 1 and 31
* 3 = Month isn't between 1 and 12
* 4 = On April, June, September and November there isn't the day 31
* 5 = On February the month has only 28 days
* 6 = Leap year, February has only 29 days


Copy this code and paste it in your HTML
  1. //+ Jonas Raoni Soares Silva
  2. //@ http://jsfromhell.com/geral/is-date [v1.0]
  3.  
  4. isDate = function(y, m, d){
  5. if(typeof y == "string" && m instanceof RegExp && d){
  6. if(!m.test(y)) return 1;
  7. y = RegExp["$" + d.y], m = RegExp["$" + d.m], d = RegExp["$" + d.d];
  8. }
  9. d = Math.abs(d) || 0, m = Math.abs(m) || 0, y = Math.abs(y) || 0;
  10. return arguments.length != 3 ? 1 : d < 1 || d > 31 ? 2 : m < 1 || m > 12 ? 3 : /4|6|9|11/.test(m) && d == 31 ? 4
  11. : m == 2 && (d > ((y = !(y % 4) && (y % 1e2) || !(y % 4e2)) ? 29 : 28)) ? 5 + !!y : 0;
  12. };

URL: http://jsfromhell.com/geral/is-date

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.