5
$\begingroup$

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..

$\endgroup$
9
  • $\begingroup$ 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. $\endgroup$ May 12, 2015 at 13:23
  • $\begingroup$ @MichaelHale Yep, DateString[{StringInsert[date, "/", {3, 5, 7}], $dateformat}, "Date"] does not look nice. $\endgroup$
    – Kuba
    May 12, 2015 at 13:25
  • $\begingroup$ 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. $\endgroup$ May 12, 2015 at 13:27
  • $\begingroup$ @MichaelHale Great. I don't feel like writing another report, I haven't got any answer for last three... $\endgroup$
    – Kuba
    May 12, 2015 at 13:29
  • 3
    $\begingroup$ I sent feedback. $\endgroup$ May 12, 2015 at 13:39

1 Answer 1

5
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Good idea, I'm surprised it works ;) p.s. you can use Riffle, it will be more compact. $\endgroup$
    – Kuba
    May 12, 2015 at 15:05
  • $\begingroup$ @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). $\endgroup$ May 12, 2015 at 17:33

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.