TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a macro that calculates the year and months from a given start date and the current date.

So let's say I have 05/02/2010 (the start date), then it should do the following: Today - 05/02/2010 => 1 year, 1 month. It guess you could add the days as well, but I don't really need that at the moment.

I'm using xelatex if that helps at all.

share|improve this question
Do you need to apply it in a large scale or do you just need a couple of values? In the latter case, I rather suggest utilising an external tool such as Wolfram Alpha. – Harold Cavendish Mar 29 '11 at 19:02
up vote 10 down vote accepted

The datenumber package allows you to calculate date differences and return the difference as number of days. You still need to make years and month out of it.

Starting of the example \daydifftoday in the manual:




      \the\numexpr-\thedatetwo/365\relax\space year(s),
      \the\numexpr(-\thedatetwo - (-\thedatetwo/365)*365)/30\relax\space month(s)



This code could be further refined to check if the difference is less than a year, one year or more than one year to display "year(s)" accordantly. The same counts for the month:




        \def\diffbefore{in }%
            \thediffyears\space years,
            \thediffyears\space year,
            \thediffmonths\space months
            \thediffmonths\space month









This gives:

1 month ago
1 year, 1 month ago
2 years, 1 month ago
2 years, 2 months ago
in 10 months
in 1 year, 8 months

This should be fine when the accuracy is months (which are taken as 30 days each). Exact days would require to take the different number of days per month and leap years into account.

Note the \diffbefore and \diffafter macros. You can adjust them to your liking and/or language.

share|improve this answer
Now what you want to do is have this command fetch the language set by babel and use the right words for that language! – Seamus Apr 1 '11 at 13:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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