Skip to content

Instantly share code, notes, and snippets.

@frayhan32
Last active December 23, 2015 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frayhan32/6578750 to your computer and use it in GitHub Desktop.
Save frayhan32/6578750 to your computer and use it in GitHub Desktop.
Check time
<script>
/**
* Checktime class
*
* Faris Rayhan 2014
*
* Profitable when dealing with time scope
*
* @param {Year} as number
* @param {Month} as number
* @param {Day} as number
*
* @return {boolean}
*/
function checkTime(Year, Month, Day) {
//Year property
if (typeof Year === "string") {
this.Year = parseFloat(Year);
}else{
this.Year = Year;
}
//Month property
if (typeof Month === "string") {
this.Month = parseFloat(Month);
}else{
this.Month = Month;
}
//Day property
if (typeof Day === "string") {
this.Day = parseFloat(Day);
} else {
this.Day = Day;
}
//Indicator property tells if our input is already valid or no. defaultly no
this.Indicator = false;
//Our check function
this.check = function () {
//Make sure all argument not empty
if(this.Year!=="" && this.Month!=="" && this.Day!==""){
//Check if month is february
if (this.Month === 2) {
//Check if leap year
if (this.Year % 4 === 0) {
if (this.Day > 29) { //If day exceeds 29 keep indicator false
} else { //But if the day equal to 29 or lower make indicator true
this.Indicator = true;
}
}
else {
if (this.Day > 28) { //This is not leap year day must 28 in maximum keep indicator false
} else { //if lower than 28 make indicator true
this.Indicator = true;
}
}
}
else {//This to validate month other than february. assign which month must 31 in maximum
if ((this.Month % 2 !== 0 || this.Month === 8 || this.Month === 10 || this.Month === 12) && (this.Month !== 9 && this.Month !== 11)) {
if (this.Day > 31) { //Keep indicator false if day exceeds 31
} else { //Other than make true if lower than 31
this.Indicator = true;
}
}
//And this where month must 30 day in maximum
else if ((this.Month % 2 === 0 || this.Month !== 8 || this.Month !== 10 || this.Month !== 12) || (this.Month === 9 && this.Month === 11)) {
if (this.Day > 30) { //Keep indicator false if day exceeds 30
} else { //Other than make true if lower than 31
this.Indicator = true;
}
}
else {
}
}
//Do conditioning for indicator. Show the final result
if (this.Indicator === false) {//Something error
return false;
}
else if (this.Indicator === true) {//Everything is okay
return true;
} else {
}
}else{
console.log('Please input all argument');
}
}
}
//Test Him
var a = new checkTime(2000, 2, 29);
console.log(a.check()); //output true
var b = new checkTime(2000, 2, 30);
console.log(b.check()); //output false
var c = new checkTime(2001, 1, 31);
console.log(c.check()); //output true
var d = new checkTime(2001, 4, 31);
console.log(d.check()); //output false
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment