Is is possible to use datetime2 to calculate the previous month from \today? (Alternatively another package).

I've found some solutions that use negative deduction of days, but that would cause problems around the end of the month.

Example: \today = 12-JUL-2017

Wanted output: "Today is 12-JUL-2017. Last month was June."

  • 1
    you can try \DTMenglishmonthname{\numexpr\month-1\relax} – touhami Jul 12 '17 at 15:45

You can save the current date using \DTMsavenow and then use \DTMfetchmonth and DTMmonthname or its equivalents to calculate the previous (or next) month. I've added both a \lastmonth and \nextmonth version.

Today is \today.
Last month was \lastmonth{today}.

Today is \DTMusedate{jan20}.
Last month was \lastmonth{jan20}.

Next month is \nextmonth{jan20}.

output of code

  • What happens if this month is January? – Nicola Talbot Jul 12 '17 at 20:41
  • 1
    @NicolaTalbot :) Oops. I'll fix soon. – Alan Munn Jul 12 '17 at 20:44
  • 1
    @NicolaTalbot Fixed. – Alan Munn Jul 12 '17 at 22:39

You can also do this without any packages. You can define your own \lastmonth macro:

enter image description here


% \setdate{<day>}{<month>}{<year>}
  \ifcase\month\or%  0
    December\or   %  1
    January\or    %  2
    February\or   %  3
    March\or      %  4
    April\or      %  5
    May\or        %  6
    June\or       %  7
    July\or       %  8
    August\or     %  9
    September\or  % 10
    October\or    % 11
    November\fi   % 12


Today is \today.
Last month was \lastmonth.

Today is \today.
Last month was \lastmonth.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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