2
$\begingroup$

I am manipulating a text file, I need to extract dates from it, that will be in the format

XJan__28_,__2020X

where

_ is either 1 or zero whitespaces __ is 1 or more than 1 whitespaces X is a combination of 0 or more whitespaces and 0 or 1 newlines

I need to get the date and calculate the time difference (in days) from now to those dates. I would need a function timedifference[string_] that does that, but I don't know how to build it, (or better, I think I could do it in a very mechanical way, I hoped some expert could show me an easy and short solution)

$\endgroup$

1 Answer 1

1
$\begingroup$

This should work for all of the specified combinations of whitespace/newline

date = "   Jan  28 ,  2020 \n"
dateObj = date // StringReplace[" " -> ""] // DateObject[{#, {"Month", "Day", "Year"}}] &
DateDifference[dateObj, Today]

(* Quantity[21, "Days"] *)
$\endgroup$
3

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.