GeneralHow to store the date

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
gsks
Posts: 2
Joined: Mon Jan 03, 2011 11:22 am

How to store the date

Post by gsks »

Dear All,
This is my first post to this forum and first of all I would like to wish all of you a Happy New Year.

Now, I have a very basic problem i latex:

My code:

\documentclass{article}
\usepackage{datetime}
\usepackage{advdate}
\begin{document}
\newdate{my_date}{\day}{\month}{\year}
\displaydate{my_date} \\
\AdvanceDate[5]
\displaydate{my_date}
\end{document}

Executed today produces:

Monday 3rd January, 2011
Saturday 8th January, 2011

I would like to store \today as a constant and be able to reference to the entry further in the document but it seems that \AdvanceDate modifies \today and my_date does not store the date before modification but a reference.

In other words, how can prevent system to change my_date?

Any suggestions appreciate.

Regards,
Gsks

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

How to store the date

Post by frabjous »

You could use a \savebox (though one problem is that this would prevent line breaks inside the date at later uses... but perhaps that's not a problem.)

Code: Select all

\documentclass{article}
\usepackage{datetime}
\usepackage{advdate}
\begin{document}
\noindent%
\newdate{my_date}{\day}{\month}{\year}%
\displaydate{my_date} \\
\newsavebox{\constantdate}\savebox{\constantdate}{\displaydate{my_date}}
\AdvanceDate[5]
\displaydate{my_date}\\
\usebox{\constantdate}
\end{document}
dates.png
dates.png (10.02 KiB) Viewed 3405 times
gsks
Posts: 2
Joined: Mon Jan 03, 2011 11:22 am

How to store the date

Post by gsks »

Thank you very much for the tip. It works. I am able to store the data. In fact the reason of my question is a task I have. I need to check if the changed date and current date are in the same month.

I modified a little the code above to store only monthname and added check condition.

Code: Select all

\documentclass{article}
\usepackage{datetime}
\usepackage{advdate}
\usepackage{ifthen}
\begin{document}
\noindent
\newsavebox{\mymonth}\savebox{\mymonth}{\monthname}
\AdvanceDate[5] \\
\ifthenelse{\equal{\usebox{\mymonth}}{\monthname}}{Dates in the same month.}{Different months: \usebox{\mymonth} vs \monthname.}
\AdvanceDate[30] \\
\ifthenelse{\equal{\usebox{\mymonth}}{\monthname}}{Dates in the same month.}{Different months: \usebox{\mymonth} vs \monthname.}
\end{document}
The above code gives me (also visible in the attached file).

Different months: January vs January.
Different monthe: January vs February.

If I use \the\months instead of \monthname the result is also false.
Why? I am confused :?

Regards,
gsks
Attachments
wynik.jpg
wynik.jpg (14.56 KiB) Viewed 3401 times
User avatar
nlct
Posts: 276
Joined: Thu Nov 06, 2008 11:15 am

How to store the date

Post by nlct »

\monthname is too complicated to compare using \equal. I suggest you store the numerical value of the month and compare that instead.

Code: Select all

\documentclass{article}

\usepackage{datetime}
\usepackage{advdate}

\begin{document}
\edef\mymonth{\number\month}
\AdvanceDate[5]
\ifnum\month=\mymonth
  Same month.
\else
  Different month: \monthname[\mymonth] vs \monthname.
\fi
\AdvanceDate[30]
\ifnum\month=\mymonth
  Same month.
\else
  Different month: \monthname[\mymonth] vs \monthname.
\fi

\end{document}
Regards
Nicola Talbot
Post Reply