Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if year is leap year in javascript [duplicate]

 function leapYear(year){     var result;      year = parseInt(document.getElementById("isYear").value);     if (years/400){       result = true     }     else if(years/100){       result = false     }     else if(years/4){       result= true     }     else{       result= false     }     return result  } 

This is what I have so far (the entry is on a from thus stored in "isYear"), I basically followed this here, so using what I already have, how can I check if the entry is a leap year based on these conditions(note I may have done it wrong when implementing the pseudocode, please correct me if I have) Edit: Note this needs to use an integer not a date function

like image 577
BigBob Avatar asked May 03 '13 06:05

BigBob


People also ask

How do you test if a year is a leap year?

Check if the number is evenly divisible by 400 to confirm a leap year. If a year is divisible by 100, but not 400, then it is not a leap year. If a year is divisible by both 100 and 400, then it is a leap year. For example, 1900 is evenly divisible by 100, but not 400 since it gives you a result of 4.75.

How do you check if a year is leap year without using any operators?

If a year is a century year, meaning divisible by 100, then it needs to be divisible by 400 to be called as a leap year. If a year is not a century year, then it needs to be divisible by 4 to be called as a leap year.

How do you know if its leap year or ordinary?

If the year can be evenly divided by 100, it is NOT a leap year, unless; The year is also evenly divisible by 400. Then it is a leap year.


2 Answers

function leapYear(year) {   return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); } 
like image 134
MMeersseman Avatar answered Oct 11 '22 21:10

MMeersseman


The function checks if February has 29 days. If it does, then we have a leap year.

ES5

function isLeap(year) {   return new Date(year, 1, 29).getDate() === 29; } 

ES6

const isLeap = year => new Date(year, 1, 29).getDate() === 29; 

Result

isLeap(1004) // true isLeap(1001) // false 
like image 25
Eugen Sunic Avatar answered Oct 11 '22 21:10

Eugen Sunic