2
$\begingroup$

I have the following format of data MM/DD/YYYY, listA={"5/1/2016", "5/2/2016", "5/3/2016", "5/4/2016", "5/5/2016", "5/6/2016", "5/7/2016", "5/8/2016", "5/9/2016", "5/10/2016", "5/11/2016", "5/12/2016", "5/13/2016", "5/14/2016", "5/15/2016", "5/16/2016", "5/17/2016", "5/18/2016", "5/19/2016", "5/20/2016", "5/21/2016", "5/22/2016", "5/23/2016", "5/24/2016", "5/25/2016", "5/26/2016", "5/27/2016", "5/28/2016", "5/29/2016", "5/30/2016", "5/31/2016"}

I require that the month and day are swapped to DD/MM/YYYY to produce listB in the same date order. Please note that the day and months have variable length of 1 or 2 digits, I need to maintain this variation.

$\endgroup$
  • 3
    $\begingroup$ Something similar to DateString[ DateList[{"10/11/2016", {"Month", "Day", "Year"}}], {"Year", "-", "Month", "-", "Day"}] will do it. There's MonthShort and DayShort for the one-digit versions. $\endgroup$ – Szabolcs Dec 15 '16 at 12:42
  • 3
    $\begingroup$ StringReplace[listA, RegularExpression["(\\d+)/(\\d+)/(\\d+)"] :> "$2/$1/$3"] ought to work. $\endgroup$ – J. M. will be back soon Dec 15 '16 at 14:49
  • 1
    $\begingroup$ @ Szabolcs, thanks that seems to work well. regards SPIL $\endgroup$ – SPIL Dec 15 '16 at 14:50
1
$\begingroup$

You could pair StringSplit and StringRiffle over "/":

StringRiffle[#, "/"] & /@ StringSplit[listA, "/"][[All, {2, 1, 3}]]

Or using an anonymous Function and StringJoin in place of Part and StringRiffle:

#2 <> "/" <> # <> "/" <> #3 & @@@ StringSplit[listA, "/"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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