How do I check which years are leap years?
Clash Royale CLAN TAG#URR8PPP
up vote
12
down vote
favorite
Can I know how to show a leap year between 2014-2020 in a Linux terminal?
Is there any way using some code like $cal
- anything to show which year is leap year between 2014-2020 straightaway?
command-line
New contributor
add a comment |Â
up vote
12
down vote
favorite
Can I know how to show a leap year between 2014-2020 in a Linux terminal?
Is there any way using some code like $cal
- anything to show which year is leap year between 2014-2020 straightaway?
command-line
New contributor
8
stackoverflow.com/questions/32196628/â¦
â Rinzwind
16 hours ago
8
echo 2016 2020
, perhaps?
â Henning Makholm
12 hours ago
The only year between 2014 and 2020 which is a leap year is 2016. You can usetest
(or[
) to tests if a number is equal to 2016.
â Abigail
6 hours ago
add a comment |Â
up vote
12
down vote
favorite
up vote
12
down vote
favorite
Can I know how to show a leap year between 2014-2020 in a Linux terminal?
Is there any way using some code like $cal
- anything to show which year is leap year between 2014-2020 straightaway?
command-line
New contributor
Can I know how to show a leap year between 2014-2020 in a Linux terminal?
Is there any way using some code like $cal
- anything to show which year is leap year between 2014-2020 straightaway?
command-line
command-line
New contributor
New contributor
edited 6 mins ago
muru
131k19275473
131k19275473
New contributor
asked 16 hours ago
lee
643
643
New contributor
New contributor
8
stackoverflow.com/questions/32196628/â¦
â Rinzwind
16 hours ago
8
echo 2016 2020
, perhaps?
â Henning Makholm
12 hours ago
The only year between 2014 and 2020 which is a leap year is 2016. You can usetest
(or[
) to tests if a number is equal to 2016.
â Abigail
6 hours ago
add a comment |Â
8
stackoverflow.com/questions/32196628/â¦
â Rinzwind
16 hours ago
8
echo 2016 2020
, perhaps?
â Henning Makholm
12 hours ago
The only year between 2014 and 2020 which is a leap year is 2016. You can usetest
(or[
) to tests if a number is equal to 2016.
â Abigail
6 hours ago
8
8
stackoverflow.com/questions/32196628/â¦
â Rinzwind
16 hours ago
stackoverflow.com/questions/32196628/â¦
â Rinzwind
16 hours ago
8
8
echo 2016 2020
, perhaps?â Henning Makholm
12 hours ago
echo 2016 2020
, perhaps?â Henning Makholm
12 hours ago
The only year between 2014 and 2020 which is a leap year is 2016. You can use
test
(or [
) to tests if a number is equal to 2016.â Abigail
6 hours ago
The only year between 2014 and 2020 which is a leap year is 2016. You can use
test
(or [
) to tests if a number is equal to 2016.â Abigail
6 hours ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
25
down vote
You can make use of date
's exit code to check for a leap year, relying on date
's behaviour of generating a non 0 exit code for an invalid date, obviosuly there's no 29th of Feb in a non-leap year:
date -d $year-02-29 &>/dev/null
echo $?
as a function:
isleap() echo is not leap;
Usage:
$ isleap 2019
is not leap
$ isleap 2020
is leap
Regarding your question:
Can i know how to show leap year between 2014-2020 in linux terminal?
echo "Leap years between 2014 and 2020:";
for y in 2014..2020; do
date -d $y-02-29 &>/dev/null && echo $y;
done
add a comment |Â
up vote
6
down vote
Just a variant of @RoVo's answer ...
for a in 2014..2020
do
date -d $a-02-29 +"%Y" 2>/dev/null
done
date -d $a-02-29 +"%Y" 2> /dev/null
sets date to the 29th of Feb and prints the year, ignoring any errors that occur.
1
Or evenseq -f "%g-02-29" 2014 2020 | date -f- +"%Y" 2>/dev/null
â Digital Trauma
6 hours ago
@DigitalTrauma: very cool!
â JJoao
4 hours ago
add a comment |Â
up vote
1
down vote
Here an elegant solution, save as isleap
:
#!/bin/bash
(( !($1 % 4) && ( $1 % 100 || !( $1 % 400) ) )) &&
echo "leap year" || echo "not a leap"
exit 0
Don't forget to set execute permission:
$ chmod +x isleap
Test it:
$ ./isleap 1900
not a leap
$ ./isleap 2000
leap year
add a comment |Â
up vote
-1
down vote
$ cat test.sh
#!/bin/sh
year=$1
y=$(( $year % 4 ))
if [ $y -eq 0 ]
then
echo "$year is a Leap Year!"
else
echo "$year is not a Leap Year!"
fi
New contributor
Divisible by 4 is the Julian way. The Gregorian way is divisible by 4, but not by 100, but yes by 400. So this will work for the years that OP wants, but fail for 1900, for example.
â wjandrea
33 mins ago
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
25
down vote
You can make use of date
's exit code to check for a leap year, relying on date
's behaviour of generating a non 0 exit code for an invalid date, obviosuly there's no 29th of Feb in a non-leap year:
date -d $year-02-29 &>/dev/null
echo $?
as a function:
isleap() echo is not leap;
Usage:
$ isleap 2019
is not leap
$ isleap 2020
is leap
Regarding your question:
Can i know how to show leap year between 2014-2020 in linux terminal?
echo "Leap years between 2014 and 2020:";
for y in 2014..2020; do
date -d $y-02-29 &>/dev/null && echo $y;
done
add a comment |Â
up vote
25
down vote
You can make use of date
's exit code to check for a leap year, relying on date
's behaviour of generating a non 0 exit code for an invalid date, obviosuly there's no 29th of Feb in a non-leap year:
date -d $year-02-29 &>/dev/null
echo $?
as a function:
isleap() echo is not leap;
Usage:
$ isleap 2019
is not leap
$ isleap 2020
is leap
Regarding your question:
Can i know how to show leap year between 2014-2020 in linux terminal?
echo "Leap years between 2014 and 2020:";
for y in 2014..2020; do
date -d $y-02-29 &>/dev/null && echo $y;
done
add a comment |Â
up vote
25
down vote
up vote
25
down vote
You can make use of date
's exit code to check for a leap year, relying on date
's behaviour of generating a non 0 exit code for an invalid date, obviosuly there's no 29th of Feb in a non-leap year:
date -d $year-02-29 &>/dev/null
echo $?
as a function:
isleap() echo is not leap;
Usage:
$ isleap 2019
is not leap
$ isleap 2020
is leap
Regarding your question:
Can i know how to show leap year between 2014-2020 in linux terminal?
echo "Leap years between 2014 and 2020:";
for y in 2014..2020; do
date -d $y-02-29 &>/dev/null && echo $y;
done
You can make use of date
's exit code to check for a leap year, relying on date
's behaviour of generating a non 0 exit code for an invalid date, obviosuly there's no 29th of Feb in a non-leap year:
date -d $year-02-29 &>/dev/null
echo $?
as a function:
isleap() echo is not leap;
Usage:
$ isleap 2019
is not leap
$ isleap 2020
is leap
Regarding your question:
Can i know how to show leap year between 2014-2020 in linux terminal?
echo "Leap years between 2014 and 2020:";
for y in 2014..2020; do
date -d $y-02-29 &>/dev/null && echo $y;
done
edited 15 hours ago
Arronical
12.7k84589
12.7k84589
answered 16 hours ago
RoVo
6,0591539
6,0591539
add a comment |Â
add a comment |Â
up vote
6
down vote
Just a variant of @RoVo's answer ...
for a in 2014..2020
do
date -d $a-02-29 +"%Y" 2>/dev/null
done
date -d $a-02-29 +"%Y" 2> /dev/null
sets date to the 29th of Feb and prints the year, ignoring any errors that occur.
1
Or evenseq -f "%g-02-29" 2014 2020 | date -f- +"%Y" 2>/dev/null
â Digital Trauma
6 hours ago
@DigitalTrauma: very cool!
â JJoao
4 hours ago
add a comment |Â
up vote
6
down vote
Just a variant of @RoVo's answer ...
for a in 2014..2020
do
date -d $a-02-29 +"%Y" 2>/dev/null
done
date -d $a-02-29 +"%Y" 2> /dev/null
sets date to the 29th of Feb and prints the year, ignoring any errors that occur.
1
Or evenseq -f "%g-02-29" 2014 2020 | date -f- +"%Y" 2>/dev/null
â Digital Trauma
6 hours ago
@DigitalTrauma: very cool!
â JJoao
4 hours ago
add a comment |Â
up vote
6
down vote
up vote
6
down vote
Just a variant of @RoVo's answer ...
for a in 2014..2020
do
date -d $a-02-29 +"%Y" 2>/dev/null
done
date -d $a-02-29 +"%Y" 2> /dev/null
sets date to the 29th of Feb and prints the year, ignoring any errors that occur.
Just a variant of @RoVo's answer ...
for a in 2014..2020
do
date -d $a-02-29 +"%Y" 2>/dev/null
done
date -d $a-02-29 +"%Y" 2> /dev/null
sets date to the 29th of Feb and prints the year, ignoring any errors that occur.
edited 11 hours ago
wjandrea
7,34042256
7,34042256
answered 11 hours ago
JJoao
1,32059
1,32059
1
Or evenseq -f "%g-02-29" 2014 2020 | date -f- +"%Y" 2>/dev/null
â Digital Trauma
6 hours ago
@DigitalTrauma: very cool!
â JJoao
4 hours ago
add a comment |Â
1
Or evenseq -f "%g-02-29" 2014 2020 | date -f- +"%Y" 2>/dev/null
â Digital Trauma
6 hours ago
@DigitalTrauma: very cool!
â JJoao
4 hours ago
1
1
Or even
seq -f "%g-02-29" 2014 2020 | date -f- +"%Y" 2>/dev/null
â Digital Trauma
6 hours ago
Or even
seq -f "%g-02-29" 2014 2020 | date -f- +"%Y" 2>/dev/null
â Digital Trauma
6 hours ago
@DigitalTrauma: very cool!
â JJoao
4 hours ago
@DigitalTrauma: very cool!
â JJoao
4 hours ago
add a comment |Â
up vote
1
down vote
Here an elegant solution, save as isleap
:
#!/bin/bash
(( !($1 % 4) && ( $1 % 100 || !( $1 % 400) ) )) &&
echo "leap year" || echo "not a leap"
exit 0
Don't forget to set execute permission:
$ chmod +x isleap
Test it:
$ ./isleap 1900
not a leap
$ ./isleap 2000
leap year
add a comment |Â
up vote
1
down vote
Here an elegant solution, save as isleap
:
#!/bin/bash
(( !($1 % 4) && ( $1 % 100 || !( $1 % 400) ) )) &&
echo "leap year" || echo "not a leap"
exit 0
Don't forget to set execute permission:
$ chmod +x isleap
Test it:
$ ./isleap 1900
not a leap
$ ./isleap 2000
leap year
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Here an elegant solution, save as isleap
:
#!/bin/bash
(( !($1 % 4) && ( $1 % 100 || !( $1 % 400) ) )) &&
echo "leap year" || echo "not a leap"
exit 0
Don't forget to set execute permission:
$ chmod +x isleap
Test it:
$ ./isleap 1900
not a leap
$ ./isleap 2000
leap year
Here an elegant solution, save as isleap
:
#!/bin/bash
(( !($1 % 4) && ( $1 % 100 || !( $1 % 400) ) )) &&
echo "leap year" || echo "not a leap"
exit 0
Don't forget to set execute permission:
$ chmod +x isleap
Test it:
$ ./isleap 1900
not a leap
$ ./isleap 2000
leap year
edited 9 hours ago
answered 9 hours ago
abu_bua
2,38241021
2,38241021
add a comment |Â
add a comment |Â
up vote
-1
down vote
$ cat test.sh
#!/bin/sh
year=$1
y=$(( $year % 4 ))
if [ $y -eq 0 ]
then
echo "$year is a Leap Year!"
else
echo "$year is not a Leap Year!"
fi
New contributor
Divisible by 4 is the Julian way. The Gregorian way is divisible by 4, but not by 100, but yes by 400. So this will work for the years that OP wants, but fail for 1900, for example.
â wjandrea
33 mins ago
add a comment |Â
up vote
-1
down vote
$ cat test.sh
#!/bin/sh
year=$1
y=$(( $year % 4 ))
if [ $y -eq 0 ]
then
echo "$year is a Leap Year!"
else
echo "$year is not a Leap Year!"
fi
New contributor
Divisible by 4 is the Julian way. The Gregorian way is divisible by 4, but not by 100, but yes by 400. So this will work for the years that OP wants, but fail for 1900, for example.
â wjandrea
33 mins ago
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
$ cat test.sh
#!/bin/sh
year=$1
y=$(( $year % 4 ))
if [ $y -eq 0 ]
then
echo "$year is a Leap Year!"
else
echo "$year is not a Leap Year!"
fi
New contributor
$ cat test.sh
#!/bin/sh
year=$1
y=$(( $year % 4 ))
if [ $y -eq 0 ]
then
echo "$year is a Leap Year!"
else
echo "$year is not a Leap Year!"
fi
New contributor
edited 26 mins ago
wjandrea
7,34042256
7,34042256
New contributor
answered 9 hours ago
Muhammad Usama
11
11
New contributor
New contributor
Divisible by 4 is the Julian way. The Gregorian way is divisible by 4, but not by 100, but yes by 400. So this will work for the years that OP wants, but fail for 1900, for example.
â wjandrea
33 mins ago
add a comment |Â
Divisible by 4 is the Julian way. The Gregorian way is divisible by 4, but not by 100, but yes by 400. So this will work for the years that OP wants, but fail for 1900, for example.
â wjandrea
33 mins ago
Divisible by 4 is the Julian way. The Gregorian way is divisible by 4, but not by 100, but yes by 400. So this will work for the years that OP wants, but fail for 1900, for example.
â wjandrea
33 mins ago
Divisible by 4 is the Julian way. The Gregorian way is divisible by 4, but not by 100, but yes by 400. So this will work for the years that OP wants, but fail for 1900, for example.
â wjandrea
33 mins ago
add a comment |Â
lee is a new contributor. Be nice, and check out our Code of Conduct.
lee is a new contributor. Be nice, and check out our Code of Conduct.
lee is a new contributor. Be nice, and check out our Code of Conduct.
lee is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1081137%2fhow-do-i-check-which-years-are-leap-years%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
8
stackoverflow.com/questions/32196628/â¦
â Rinzwind
16 hours ago
8
echo 2016 2020
, perhaps?â Henning Makholm
12 hours ago
The only year between 2014 and 2020 which is a leap year is 2016. You can use
test
(or[
) to tests if a number is equal to 2016.â Abigail
6 hours ago