Message Boards Message Boards

0
|
2477 Views
|
3 Replies
|
4 Total Likes
View groups...
Share
Share this post:

How can I convert a list of dates?

Posted 3 years ago

Hi, sorry for the dumb question (I'm still studying). I have an array of dates like

Dates={"2020-02-24T18:00:00", "2020-02-25T18:00:00", "2020-02-26T18:00:00"}

I would like to convert all of them to a specified format. If I write

DateString[Dates[[1]], {"Day", "/", "Month", "/", "YearShort"}]<br>

I get the correct result:

24/02/20

I'm however unable to convert the whole set of dates to

{24/02/20, 25/02/20, 26/02/20}

Here is the code snippet:

link = "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-andamento-nazionale/dpc-covid19-ita-andamento-nazionale.csv";
ita = Transpose[
   Cases[Import[link], {_, 
     "ITA", _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _}]];
Dates = ita[[1]]

Thanks!

3 Replies
Posted 3 years ago

Thanks!
I like a lot Mathematica, because it allows to work on on objects collectively ("if you write a loop probably you are doing the wrong thing").

POSTED BY: Updating Name

With DateObject it may be easier to manipulate further:

Map[DateObject[#, 
   DateFormat -> {"Day", "/", "Month", "/", "YearShort"}] &, ita[[1]]]
POSTED BY: Gianluca Gorni
Posted 3 years ago

Hi Teodoro,

Here are a couple of ways to do it

DateString[#, {"Day", "/", "Month", "/", "YearShort"}] & /@ dates
(* {"24/02/20", "25/02/20", "26/02/20"} *)

dates /. d_String :> DateString[d, {"Day", "/", "Month", "/", "YearShort"}]
(* {"24/02/20", "25/02/20", "26/02/20"} *)
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract