# Leap year calculation

year=2010; math=`echo "\$year%4" | bc`; [ ! -z \$year ] && [ \$math -eq 0 ] && echo "\$year is leap year!" || echo "\$year isn't leap year";

-1
2010-03-30 15:33:00

## 2 Alternatives + Submit Alt

• 2
leapyear() { [ \$(date -d "Dec 31, \$1" +%j) == 366 ] && echo leap || echo not leap; }
· 2010-03-30 20:13:56
• Tested on bash, and follows all the rules about leap years. Show Sample Output

-2
leapyear() { if [ \$[\$1 % 4] -eq 0 ] && [ \$[\$1 % 100] -ne 0 ] || [ \$[\$1 % 400] -eq 0 ]; then echo \$1' is a leap year!'; else echo \$1' is not a leap year.'; fi; }
· 2010-03-30 17:19:20

### What Others Think

This doesn't work for all leap years. If a year is evenly divisible by 4 and 100, but not 400, then it's not a leap year. For example, 2200 is divisible by 4, but it's not a leap year. Here's a bash function that takes a year as an argument and returns whether or not it's a leap year. `leapyear(){ [ \$[\$1%400] -eq 0 ]&&{ echo \$1' is a leap year!';return;};[ \$[\$1%100] -eq 0 ]&&{ echo \$1' is not a leap year.';return;};[ \$[\$1%4] -eq 0 ]&&echo \$1' is a leap year!'||echo \$1' is not a leap year.';}`
eightmillion · 517 weeks and 1 day ago

### What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

### What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.