javascript - How do I validate yyyy-mm-dd hh:mm:ss format -


i saw this fiddle validating mm/dd/yyyy or mm-dd-yyyy validate yyyy-mm-dd hh:mm:ss format how ensure today lesser date yyyy-mm-dd hh:mm:ss format?.

this how have initiated date time picker..

$("#startdate, #enddate").datetimepicker({ dateformat: 'yyyy-mm-dd hh:mm:ss'});  

please me done.

thanks

the date format have specified iso 8601. modern browsers support date parsing of string format. can this.

javascript

var iso8601 = "2013-02-01 10:00:00",     userdate = new date(iso8601),     today = new date(),     datetime,     date,     time,     value;  // check valid date if (isnan(userdate)) {     alert("invalid userdate"); }  // check if userdate before today if (userdate.getdate() < today.getdate()) {     alert("userdate in past"); }  // check string matches "yyyy-mm-dd hh:mm:ss" , valid function isgregorianleapyear(year) {     return year % 400 === 0 || year % 100 !== 0 && year % 4 === 0; }  function daysingregorianmonth(year, month) {     var days;      if (month == 2) {         days = 28;         if (isgregorianleapyear(year)) {             days += 1;         }     } else {         days = 31 - ((month - 1) % 7 % 2);     }      return days; }  if (typeof iso8601 !== "string") {     alert("not iso8601 string"); } else {     datetime = iso8601.split(" ");     if (datetime.length !== 2) {         alert("missing date or time element");     } else {         date = datetime[0].split("-");         if (date.length !== 3) {             alert("incorrect number of date elements");         } else {             value = +date[0];             if (date[0].length !== 4 || value < -9999 || value > 9999) {                 alert("year value incorrect");             }              value = +date[1];             if (date[1].length !== 2 || value < 1 || value > 12) {                 alert("month value incorrect");             }              value = +date[2];             if (date[2].length !== 2 || value < 1 || value > daysingregorianmonth(+date[0], +date[1])) {                 alert("day value incorrect");             }         }          time = datetime[1].split(":");         if (time.length !== 3) {             alert("incorrect number of time elements");         } else {             value = +time[0];             if (time[0].length !== 2 || value < 0 || value > 23) {                 alert("hour value incorrect");             }              value = +time[1];             if (time[1].length !== 2 || value < 0 || value > 59) {                 alert("minute value incorrect");             }              value = +time[2];             if (time[2].length !== 2 || value < 0 || value > 59) {                 alert("second value incorrect");             }         }     } } console.log(userdate); console.log(today); 

jsfiddle


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -