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!


5 Answers 5


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")



1425 days ago

724 days from now
  • 1
    \$\begingroup\$ +1 for using "days only" interpretation \$\endgroup\$ Commented 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
    Commented Dec 9, 2013 at 4:39
  • \$\begingroup\$ @flodel Thanks for the tip! Great idea to assign the value inside if. \$\endgroup\$ Commented Dec 9, 2013 at 8:22
  • \$\begingroup\$ Abuse is within the rules of code golf. \$\endgroup\$
    – user8777
    Commented 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
    Commented Dec 9, 2013 at 14:06

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.


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

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}"}


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.


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()
alert(e?[d/365|0,"years,",d%365/7|0,"weeks,",e,"days",D>0?"ago":"from now"].join(" "):"Today")

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")));}


#include <time.h>
main (int a,char *v[])
  struct tm t={0};
  char* n=strtok(v[1],"/");
                case 1:t.tm_mon=atoi(n)-1;
                case 2:t.tm_mday=atoi(n);
                case 3:t.tm_year=atoi(n)-1900;
  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
  • 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\$ Commented 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\$ Commented Dec 10, 2013 at 7:40
  • \$\begingroup\$ @JohannesKuhn Done. That reduced the character count by a ton, thanks! \$\endgroup\$
    – syb0rg
    Commented Dec 10, 2013 at 15:00

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*
print[w[1:]+" ago","today",w+" from now"][cmp(D,t)+1]

Your Answer

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

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