-7
\$\begingroup\$

Create a program that, given an integer N, outputs the number of years we have to get to N A.D. or how many years it has been since N A.D, as a string or int. For example, given 2014, the program should output 5 as it has been that many years since 2014. Input of the year in which you run the program should result in 0.

Treat the input "0" however you like: as its own year, as 1 A.D. or as 1 B.C. This means that you can pretend there IS a year zero and have all BC years be off by one.

Negative integers (integers with - in front of them) are treated as years B.C. For example inputting -2 would result in the program telling how many years it has been since 2 B.C.

Treat improper inputs (inputs that don't only use numbers and the dash as characters (abcd), or use the dash by itself or use it wrongly (e.g. 6-)) however you want, error or exit or something else.

The winning program is the program with the shortest code that does all of this. Have fun.

\$\endgroup\$

closed as unclear what you're asking by Xcali, Luis felipe De jesus Munoz, HyperNeutrino, Giuseppe, mbomb007 Jun 13 at 19:01

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ Am I dumb or has it been 5 years since 2014? \$\endgroup\$ – HyperNeutrino Jun 13 at 14:07
  • 1
    \$\begingroup\$ @HyperNeutrino it has \$\endgroup\$ – Luis felipe De jesus Munoz Jun 13 at 14:08
  • 1
    \$\begingroup\$ So an input of -1 should result in an output of 2019 this year? \$\endgroup\$ – Neil Jun 13 at 14:25
  • \$\begingroup\$ @HyperNeutrino maybe the thinking is that only 4 full calendar years have passed since the end of 2014. So what is the output meant to be for 2009?.. \$\endgroup\$ – Jonathan Allan Jun 13 at 14:28
  • 2
    \$\begingroup\$ @JonathanAllan Hm. Well, OP posted and probably left, so hopefully they clarify, because otherwise I'm tempted to CV this because there are like three reasonable interpretations plus the answer of "OP forgot it was 2019" \$\endgroup\$ – HyperNeutrino Jun 13 at 14:33

10 Answers 10

2
\$\begingroup\$

05AB1E, 3 bytes

žgα

Try it online!

Explanation

  α   # absolute difference between
      # implicit input
žg    # and current year
\$\endgroup\$
2
\$\begingroup\$

Java 8, 46 bytes

n->(n-=java.time.Year.now().getValue())<0?-n:n

Try it online.

Or alternatively:

n->Math.abs(n-java.time.Year.now().getValue())

Try it online.

\$\endgroup\$
1
\$\begingroup\$

Japt, 3 bytes

aKi

Try it

aKi     :Implicit input of year
a       :Absolute difference with
 K      :  Date
  i     :  Get year
\$\endgroup\$
  • \$\begingroup\$ lol, we posted the same answer at the same time. I deleted mine tho, i had another in php anyway \$\endgroup\$ – Luis felipe De jesus Munoz Jun 13 at 14:14
  • \$\begingroup\$ @LuisfelipeDejesusMunoz - technically, you got there first ... by 8 seconds! No need to delete either of them, though; it happens sometimes. \$\endgroup\$ – Shaggy Jun 13 at 14:18
1
\$\begingroup\$

Pyth, 4 bytes

a.d3

Try it online!

Explanation:

a.d3Q - full program. implicit Q added.
a     - absolute difference of
 .d3  - current year and
    Q - implicit input
\$\endgroup\$
0
\$\begingroup\$

PHP, 22 bytes

<?=date('Y')-$argv[1];

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 3, 66 bytes

from datetime import*
print(abs(int(input())-datetime.now().year))

Try it online!

Gives 5 for 2014 because it's 2019, not 2018.

\$\endgroup\$
0
\$\begingroup\$

Red, 30 bytes

func[y][absolute y - now/year]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C# (Visual C# Interactive Compiler) with /u:System.Math and /u:System.DateTime flags, 32 18 bytes

n=>Abs(Now.Year-n)

Try it online.

\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 33 bytes

a=>Math.abs(Date().split` `[3]-a)

Try it online!

-6 bytes thanks to Luis felipe De jesus Munoz

\$\endgroup\$
  • \$\begingroup\$ @LuisfelipeDejesusMunoz Oh thanks! \$\endgroup\$ – HyperNeutrino Jun 13 at 15:57
0
\$\begingroup\$

C++ (gcc), 131 127 bytes

#include<ctime>
#include<cstdio>
int main(){long y,t=time(0);scanf("%d",&y);y-=gmtime(&t)->tm_year+1900;printf("%d",y<0?-y:y);}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Thanx for the hint, found an even better solution :) \$\endgroup\$ – movatica Jun 14 at 22:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.