JAVA SCRIPT TO VALIDATE TIME IN : DD-MM-YYYY HH:MM:SS FORMAT

function validDateTime_DD_MM_YYYY_HH_MM_SS() {
//This function will validte the datetimes of this format : dd-mm-yyyy hh:mm:ss
var date = document.getElementById(“startTime”).value.trim();
var valid = true;
var spaceIndex = date.indexOf(” “);
var onlyDate = date.substring(0,spaceIndex);
var dateData = onlyDate.split(“-“);
var onlyTime = date.substring(spaceIndex);
var timeData = onlyTime.split(“:”);
var day = parseInt(dateData[0]);
var month = parseInt(dateData[1]);
var year = parseInt(dateData[2]);
var hour = parseInt(timeData[0]);
var min = parseInt(timeData[1]);
var sec = parseInt(timeData[2]);
var reg = new RegExp(‘[0-9]$’);

if(!reg.test(day) || !reg.test(month) || !reg.test(year) || !reg.test(hour) || !reg.test(min) ||                           !reg.test(sec)) valid =false;

else if((month < 1) || (month > 12)) valid = false;

else if((day < 1) || (day > 31)) valid = false;
else if(((month == 4) || (month == 6) || (month == 9) || (month == 11)) && (day > 30)) valid =          false;
else if((month == 2) && (((year % 400) == 0) || ((year % 4) == 0)) && ((year % 100) != 0) &&          (day > 29)) valid = false;
else if((month == 2) && ((year % 100) == 0) && (day > 29)) valid = false;
else if((hour < 0) || (hour > 24)) valid = false;
else if((min < 0) || (min > 59)) valid = false;
else if((sec < 0) || (sec > 59)) valid = false;

return valid;
}

Advertisements

About rajkirpal

i am a software engineer. and i have decent knowledge about java technologies like ANDROID, Servlet, Jsp, Struts, Hibernet, Spring, J2ME, Java Script, Ajax, JQuery, CSS. Further i am Preparing Easy understandable materials/documents upon the same java technologies and developing project in core java , advanced java and j2me , ANDROID as per the requirements Also designing the web sites and logos Thank You.
This entry was posted in JavaScript / CSS, JQuery. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s