How do I check which years are leap years?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question









New contributor




lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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














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?










share|improve this question









New contributor




lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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












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?










share|improve this question









New contributor




lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 6 mins ago









muru

131k19275473




131k19275473






New contributor




lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 16 hours ago









lee

643




643




New contributor




lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 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












  • 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







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










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





share|improve this answer





























    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.






    share|improve this answer


















    • 1




      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

















    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





    share|improve this answer





























      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





      share|improve this answer










      New contributor




      Muhammad Usama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.

















      • 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










      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "89"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      lee is a new contributor. Be nice, and check out our Code of Conduct.









       

      draft saved


      draft discarded


















      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






























      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





      share|improve this answer


























        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





        share|improve this answer
























          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





          share|improve this answer














          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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 15 hours ago









          Arronical

          12.7k84589




          12.7k84589










          answered 16 hours ago









          RoVo

          6,0591539




          6,0591539






















              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.






              share|improve this answer


















              • 1




                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














              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.






              share|improve this answer


















              • 1




                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












              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.






              share|improve this answer














              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.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 11 hours ago









              wjandrea

              7,34042256




              7,34042256










              answered 11 hours ago









              JJoao

              1,32059




              1,32059







              • 1




                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












              • 1




                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







              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










              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





              share|improve this answer


























                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





                share|improve this answer
























                  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





                  share|improve this answer














                  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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 9 hours ago

























                  answered 9 hours ago









                  abu_bua

                  2,38241021




                  2,38241021




















                      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





                      share|improve this answer










                      New contributor




                      Muhammad Usama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.

















                      • 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














                      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





                      share|improve this answer










                      New contributor




                      Muhammad Usama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.

















                      • 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












                      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





                      share|improve this answer










                      New contributor




                      Muhammad Usama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      $ 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






                      share|improve this answer










                      New contributor




                      Muhammad Usama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      share|improve this answer



                      share|improve this answer








                      edited 26 mins ago









                      wjandrea

                      7,34042256




                      7,34042256






                      New contributor




                      Muhammad Usama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      answered 9 hours ago









                      Muhammad Usama

                      11




                      11




                      New contributor




                      Muhammad Usama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





                      New contributor





                      Muhammad Usama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






                      Muhammad Usama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.











                      • 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




                      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










                      lee is a new contributor. Be nice, and check out our Code of Conduct.









                       

                      draft saved


                      draft discarded


















                      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.













                       


                      draft saved


                      draft discarded














                      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













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      What does second last employer means? [closed]

                      One-line joke