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



_ 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)


2 Answers 2


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"] *)

Since V 12.3 we have FromDateString:

date = "   Jan  28 ,  2020 \n";

dob = FromDateString @ date

enter image description here

DateDifference[dob, Today]

Quantity[1372, "Days"]


Your Answer

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

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