# Handy dates conversion

I feel like something very basic is missing me,

$dateformat = {"Year", "Month", "Day", "Hour"}; date = DateString[$dateformat]

DateString["Date"]

"2015051215"
"Tuesday 12 May 2015"


How to get what DateString["Date"] gives using $dateformat and date? According to the last example in documentation I thought this should work: DateString[{date,$dateformat},"Date"]


DateString::str: String 2015051215 cannot be interpreted as a date in format {Year,Month,Day,Hour}. >>

but it isn't..

• My initial thoughts are that it's a bug. The documentation leads you to think this will work: DateList[{"1505", {"YearShort", "Month"}}]. However, you need a character like "/" between the YearShort and Month. May 12, 2015 at 13:23
• @MichaelHale Yep, DateString[{StringInsert[date, "/", {3, 5, 7}], $dateformat}, "Date"] does not look nice. – Kuba May 12, 2015 at 13:25 • Based on the behavior of DateList[{"1505", {"YearShort"}}] I think YearShort automatically switches to Year if it finds more than two numeric characters in a row. So then it's complaining that it doesn't find a Month. May 12, 2015 at 13:27 • @MichaelHale Great. I don't feel like writing another report, I haven't got any answer for last three... – Kuba May 12, 2015 at 13:29 • I sent feedback. May 12, 2015 at 13:39 ## 1 Answer Here's a simple fix/workaround. It appears that all you need to do to make your code work as intended is to make the following change to your $dateFormat assignment:

$dateFormat = {"Year", "", "Month", "", "Day", "", "Hour"}  This generates an identical string to your test example, but it appears that the empty strings are necessary to correctly parse the resulting text. Using this modified date format, DateString[{"2015051210",$dateFormat}, "Date"]


returns

"Tuesday 12 May 2015"


as desired.

• Good idea, I'm surprised it works ;) p.s. you can use Riffle, it will be more compact.
– Kuba
May 12, 2015 at 15:05
• @Kuba Yes I was a little surprised myself. Definitely curious behaviour, but perhaps you have to make it explicit that there are no delimiters in the string. I agree that Riffle makes things more compact (and robust). May 12, 2015 at 17:33