# Converting string date to date object and retriving time difference

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)

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

• Thanks. DataObject is the function I needed Feb 18 '20 at 17:57
• Does not work, "DateObject::str: String Jan28,2020 cannot be interpreted as a date." Feb 19 '20 at 11:04
• What version are you running. Works fine on V12. wolframcloud.com/obj/ac3393d1-5728-4d45-837b-e537f114001e Feb 19 '20 at 17:16