1
$\begingroup$

I have a list of dates in the form YYYY-MM-DD, where each element is not surrounded by quotations. How do I use Mathematica to convert that list of dates into a list of the number of days since a specific date? Here's an example:

Let's say I have the following list of dates

l={1997-05-16, 1997-05-19, 1997-05-20, 1997-05-21, 1997-05-22}

and I want a list of days since 1997-05-16 for each element in l. So, I would want the output

{0, 3, 4, 5, 6}


I know the function DateDifference exists, but it only takes a maximum of two elements and doesn't accept lists; plus, the values must be of the form {YYYY, MM, DD} or strings like "1997-05-19" (must have quotations).

EDIT:
Regarding the date format, these dates are coming from an imported CSV file. I tried to look for a way to make all the elements strings, i.e. insert quotations around each element in the list, but I could not find a way to do that. Is there a way to do that? I have Mathematica v9.0.1

EDIT 2:
Here is the CSV data on pastebin: https://pastebin.com/BRqwfGPi

Additionally, here is how I'm importing the date data:

datedata = 
  Import["CSV Data", {"Data", All, 1}];(* Imports dates from first column in CSV *)
datedata1 = Delete[datedata, 1];(* Deletes header element from list *)

Note that I replaced the actual path to the file as "CSV Data" in Import.

$\endgroup$
5
  • 4
    $\begingroup$ 1997-05-16 is a math formula and won't survive long unless you use Hold or something. Have you seen l after you defined it? Where do you get the input from? $\endgroup$
    – Kuba
    Apr 9, 2018 at 12:02
  • $\begingroup$ If those dates of yours are not strings, they aren't in a format compatible with Mathematica. Maybe talk about where you're getting these dates from? $\endgroup$ Apr 9, 2018 at 12:08
  • $\begingroup$ @J.M. See my edited post above $\endgroup$ Apr 9, 2018 at 23:54
  • $\begingroup$ You saying "these dates are coming from an imported CSV file" and "I tried to look for a way to make all the elements strings" is telling me that you may not be doing something right with the Import[]. Why not post the CSV on Pastebin, and then show in your question how you do the importing? $\endgroup$ Apr 10, 2018 at 1:47
  • $\begingroup$ @J.M. See my second edit above $\endgroup$ Apr 10, 2018 at 3:40

2 Answers 2

2
$\begingroup$

Here's how to do it, starting from the CSV file:

dates = Rest[Import["https://pastebin.com/raw/BRqwfGPi", {"Data", All, 1}]];

Table[QuantityMagnitude[DateDifference["1997-05-16", date, "Days"]], {date, dates}]
// Short
   {0, 3, 4, 5, 6, 7, 11, 12, 13, 14, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, 32, 33,
    34, 35, 38, 39, 40, 41, 42, 45, <<5091>>, 7441, 7444, 7445, 7446, 7447, 7448, 7451,
    7452, 7453, 7454, 7455, 7458, 7459, 7460, 7461, 7462, 7465, 7466, 7467, 7468, 7469,
    7472, 7473, 7474, 7475, 7476, 7479, 7480, 7481, 7482, 7483}
$\endgroup$
1
  • $\begingroup$ Thank you so much! This is what I was looking for $\endgroup$ Apr 10, 2018 at 4:35
2
$\begingroup$
dlF = Function[{x}, AbsoluteTime @ ToString @ Unevaluated @ x, {HoldFirst, Listable}];
dl = dlF @ {1997-05-16, 1997-05-19, 1997-05-20, 1997-05-21, 1997-05-22};

QuantityMagnitude @ DateDifference[First @ dl, #]& /@ dl

{0., 3., 4., 5., 6.}

$\endgroup$
1
  • $\begingroup$ This solution works as well, thank you $\endgroup$ Apr 10, 2018 at 4:37

Your Answer

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

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