2
\$\begingroup\$

Given a specific date, have the code output a relative reference:

  1. If the date entered is ahead of the current date, express as "x days from now" or "x weeks, y days from now", or "x years, y weeks, z days from now". Zero units need not be mentioned.
  2. If the date is the current simply have it output "Today"
  3. If the date entered is earlier than the current date, express as "x days ago" or "x weeks, y days ago", or "x years, y weeks, z days ago". Zero units need not be mentioned.

Smallest solutions are the winners here...

Good luck!

\$\endgroup\$

5 Answers 5

5
\$\begingroup\$

R, 102, 98

(kinda cheating: literal interpretation of the question)

cat(if(x<-as.Date(readline())-Sys.Date())c(abs(x),"days",c("from now","ago")[(x<0)+1])else"today")

Examples:

2013-12-08
today

2010-01-13
1425 days ago

2015-12-2
724 days from now
\$\endgroup\$
5
  • 1
    \$\begingroup\$ +1 for using "days only" interpretation \$\endgroup\$ Dec 9, 2013 at 4:07
  • 1
    \$\begingroup\$ can squeeze a few: cat(if(x<-as.Date(readline())-Sys.Date())c(abs(x),"days",c("from now","ago")[(x<0)+1])else"today") \$\endgroup\$
    – flodel
    Dec 9, 2013 at 4:39
  • \$\begingroup\$ @flodel Thanks for the tip! Great idea to assign the value inside if. \$\endgroup\$ Dec 9, 2013 at 8:22
  • \$\begingroup\$ Abuse is within the rules of code golf. \$\endgroup\$
    – user8777
    Dec 9, 2013 at 8:22
  • 1
    \$\begingroup\$ Hey, I was the first to use the literal interpretation and I was shorter :P \$\endgroup\$
    – Doorknob
    Dec 9, 2013 at 14:06
4
\$\begingroup\$

Ruby, kinda cheating - 89, less cheaty - 140

Kinda cheating, 89

f=->d{i=((d-Time.now)/86400).ceil;["Today","#{-i} days ago","#{i} days from now"][0<=>i]}

It's a function that takes the date as an argument and returns the string.

Examples:

irb(main):036:0> f[Time.new(2013, 12, 1)]
=> "7 days ago"
irb(main):037:0> f[Time.new(2013, 12, 10)]
=> "2 days from now"
irb(main):038:0> f[Time.new(2013, 12, 8)]
=> "Today"
irb(main):039:0> f[Time.new(2011, 10, 8)]
=> "792 days ago"

Hey, you never said we couldn't use only days!

express as "x days from now" or "x weeks, y days from now", or "x years, y weeks, z days from now". [emphasis mine]

Well, I choose the first one :P Abusing the rules is so fun!

Less cheaty, 140

f=->d{t=Time.new
a=((d-t)/86400).ceil
g='from now'
a,g=-a,'ago'if a<0
a==0?'Today':"#{a/365} years, #{a%365/7} weeks, #{a%365%7} days #{g}"}

Examples:

irb(main):111:0> f[Time.new 2013, 12, 10]
=> "0 years, 0 weeks, 2 days from now"
irb(main):112:0> f[Time.new 2013, 12, 17]
=> "0 years, 1 weeks, 2 days from now"
irb(main):113:0> f[Time.new 2014, 12, 17]
=> "1 years, 1 weeks, 2 days from now"
irb(main):114:0> f[Time.new 2012, 12, 17]
=> "0 years, 50 weeks, 6 days ago"
irb(main):115:0> f[Time.new 2012, 1, 1]
=> "1 years, 48 weeks, 6 days ago"
irb(main):116:0> f[Time.new 2013, 12, 8]
=> "Today"

It said

Zero units need not be mentioned.

But you never said they cannot be mentioned.

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

JavaScript, 146

Input is a timestamp as number of milliseconds from 1970-01-01 (Unix timestamp × 1000), since this makes things more conventient for me (and no input format was specified).

D=new Date-prompt()
d=Math.abs(D/864e5)
e=d%365%7|0
alert(e?[d/365|0,"years,",d%365/7|0,"weeks,",e,"days",D>0?"ago":"from now"].join(" "):"Today")
\$\endgroup\$
2
\$\begingroup\$

C - 348

First CodeGolf post, so I figured that I would have fun with it.

#include<time.h>main(int a,char *v[]){a--;struct tm t={0};char* n=strtok(v[1],"/");while(n!=0){switch(a++){case 1:t.tm_mon=atoi(n)-1;case 2:t.tm_mday=atoi(n);case 3:t.tm_year=atoi(n)-1900;}n=strtok(0,"/");}a=(int)difftime(time(0),mktime(&t))/86400;printf("%s\n",a=0?"today":(a>0?strcat(itoa(a)," days ago"):strcat(itoa(abs(a))," days from now")));}

Ungolfed:

#include <time.h>
main (int a,char *v[])
{
  a--;
  struct tm t={0};
  char* n=strtok(v[1],"/");
  while(n!=0)
  {
    switch(a++)
    {
                case 1:t.tm_mon=atoi(n)-1;
                case 2:t.tm_mday=atoi(n);
                case 3:t.tm_year=atoi(n)-1900;
    }
    n=strtok(0,"/");
  }
  a=(int)difftime(time(0),mktime(&t))/86400;
  printf("%s\n",a=0?"today":(a>0?strcat(itoa(a)," days ago"):strcat(itoa(abs(a))," days from now")));
}

Test runs:

$ ./date 12/8/2013
1 days ago
$ ./date 12/10/2013
1 days from now
$ ./date 12/9/2013
today
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Nice work, now the next thing you need to do is compress that sucker as much as you can to reduce your characters, which in turn improves your score... :) \$\endgroup\$ Dec 10, 2013 at 7:11
  • \$\begingroup\$ Please remove the unnesscary whitespace - you can post an ungolfed version too. If you post both, I'll upvote it. \$\endgroup\$ Dec 10, 2013 at 7:40
  • \$\begingroup\$ @JohannesKuhn Done. That reduced the character count by a ton, thanks! \$\endgroup\$
    – syb0rg
    Dec 10, 2013 at 15:00
1
\$\begingroup\$

Python 2 - 134

This is a bit abusive as this assumes the objects given is a datetime.date() object. But you did say it would be given as a date, so...

from datetime import*
D=input()
t=datetime.now().date()
w=str(D-t).split(',')[0]
print[w[1:]+" ago","today",w+" from now"][cmp(D,t)+1]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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