Skip to content

Instantly share code, notes, and snippets.

@muthu32
Created July 5, 2017 08:21
Show Gist options
  • Save muthu32/11efa858d53340ec720a61bfc3aa2450 to your computer and use it in GitHub Desktop.
Save muthu32/11efa858d53340ec720a61bfc3aa2450 to your computer and use it in GitHub Desktop.
function isValidDate(date)
{
var matches = /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{4})$/.exec(date);
if (matches == null) return false;
var d = matches[1];
var m = matches[2] - 1;
var y = matches[3];
var composedDate = new Date(y, m, d);
return composedDate.getDate() == d &&
composedDate.getMonth() == m &&
composedDate.getFullYear() == y;
}
//valid date including leap years
function isValidLeapDate(date) {
var valid = true;
date = date.replace('/-/g', '');
var month = parseInt(date.substring(0, 2),10);
var day = parseInt(date.substring(2, 4),10);
var year = parseInt(date.substring(4, 8),10);
if(isNaN(month) || isNaN(day) || isNaN(year)) return false;
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((month == 2) && (day > 28)) valid = false;
return valid;
}
function isvalidRegEx(subject){
if (subject.match(/^(?:(0[1-9]|[12][0-9]|3[01])[\- \/.](0[1-9]|1[012])[\- \/.](19|20)[0-9]{2})$/)){
return true;
}else{
return false;
}
}
function getjsDate(date)
{
var matches = /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{4})$/.exec(date);
if (matches == null) return false;
var d = matches[1];
var m = matches[2] - 1;
var y = matches[3];
var composedDate = new Date(y, m, d);
return composedDate;
}
//http://www.htmlgoodies.com/html5/javascript/calculating-the-difference-between-two-dates-in-javascript.html
function getdiffDate(date1, date2 )
{
//Get 1 day in milliseconds
var one_day=1000*60*60*24;
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = date2_ms - date1_ms;
// Convert back to days and return
return Math.round(difference_ms/one_day);
}
function increasedatebyDays(date,days)
{
var temp = date;
temp.setTime( temp.getTime() + days * 86400000 );
return temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment