0

I try to only display items from the csv file for which the date lies in the futere

\begin{tabular}{rlll} % Date&&&\\\ 
\csvreader[
filter test=\ifnumgreater{\datum}{\today},]%
{test.csv}{date=\datum,time=\time,titel=\titel, price=\price}%
{ \datum &\time &\checksum\,€ &\titel \\}%
\end{tabular}

The date has the format 01.01.2019. I tried a lot to parse the date but it does not work in the filter test.

  • 1
    Could you add a fully-compilable MWE? – Raaja Dec 13 '18 at 12:46
  • The test is \ifnumgreater{\datum}{\today}...which can never work. You said the \datum format is 01.01.2019, but the default formation for \today is January 01, 2018 (on my machine) and is dependent upon the date format of your machine...which may not be DD.MM.YYYY. Regardless, this will fail because you are trying to compare two strings with \ifnumgreater which compares numbers. – whatisit Dec 13 '18 at 22:24
  • If you are able to have the .csv file separate day, month, and year, as separated by commas instead of periods, then you will have a much easier solution available. If you cannot change the .csv file... then you need to parse the dates within the \csvreader call and then do a comparison. Rergarding the approach, you will need to compare year first, then month, then day, or find a pre-existing date compare command. – whatisit Dec 13 '18 at 22:29
1

This answer goes off of the assumption that you can modify your input file. Instead of using DD.MM.YYYY format, I will assume that you can manage DD,MM,YYYY format instead (i.e. using commas instead of periods). As I mentioned in my comment, if you must keep the format as DD.MM.YYYY, then it will require parsing the string and lots of extra steps.

The code sample provided seems a little strange to me, because of \checksum. You didn't get it from the file via \csvreader, so I have ignored that here. The csvsimple documentation also provides a different way to make tables, so I used there here.

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents*}{test.csv}
01,01,2019,00:00:00,t1,2
05,02,2019,00:00:01,t2,4
06,03,2020,00:00:02,t3,6
07,08,2018,00:00:03,t4,7
31,12,2018,00:00:04,t5,8
\end{filecontents*}

\usepackage[official]{eurosym}
\usepackage{etoolbox}
\usepackage{csvsimple}

\begin{document}

\makeatletter
\csvreader[no head,
            tabular=rlll,
            filter test=
\ifboolexpr{%
    test {\ifnumgreater{\entryyear}{\number\year}}%Year is greater
    or
    (test{\ifnumequal{\entryyear}{\number\year}}%
        and test{\ifnumgreater{\entrymonth}{\two@digits{\the\month}}})%Year is equal, but month is greater
    or
    (test{\ifnumequal{\entryyear}{\number\year}}%
        and test{\ifnumequal{\entrymonth}{\two@digits{\the\month}}}%
        and test{\ifnumgreater{\entryday}{\two@digits{\the\day}}})%Year is equal, month is equal, but day is greater
}%
]%
{test.csv}%
{1=\entryday, 2=\entrymonth, 3=\entryyear, 4=\entrytime, 5=\entrytitle, 6=\entryprice}%
{ \entryday.\entrymonth.\entryyear & \entrytime & \entryprice\,\euro{} & \entrytitle }%
\makeatother

\end{document}

I included the file contents that I worked with. Because you provided no examples of your .csv file, you may need to modify this example accordingly.

You'll notice that \makeatletter...\makeatother is used. This is because I used \two@digits. You could make another command with \two@digits{\the\month} and \two@digits{\the\day} if you want to avoid using @.

The most important change was including etoolbox package and modifying the filter test content. Basically, you have three numbers and you can compare them with today's date. The comparison is done inside \ifboolexpr. The are three chunks:

  • If the year is greater than the current year, then the date is greater
  • If the year is equal (to today), but the month is greater, then the date is greater
  • If the year and month are equal (to today), but the day is greater, then the date is greater

All other conditions indicate that the date read from the .csv file is either the same or earlier in time (i.e. less). So, they do not appear.

The resulting output is this image below. (You will notice that the forth data entry in the file contents does not appear, because it is earlier than today's date).

csvsimple with filter to remove current/older dates but keep future dates

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.