jinni
jinni

Reputation: 373

r Date conversion specific format

I have a date in POSIXlt format:

mydate

[1] "2011-12-25 15:47:21"

and I want a character string "12/25/2011"

I have tried using "mydate$mon+1/mydate$mday/mydate$year+1900" but I have problems making R evaluate the right terms.

Also, I tried as.Date with format="%m/%d/%y" but it ignored that format and output

as.Date(mydate)

[1] "2011-12-25"

Thanks,

Upvotes: 0

Views: 326

Answers (2)

Ramnath
Ramnath

Reputation: 55685

I am going to put in a shameless self-plug for an R package I am developing, which will do this automatically. Version 0.0.1 is available here https://github.com/ramnathv/intellidate

library(intellidate)
new <- as.POSIXlt("2011-12-25 15:47:21")
str_to_date(new)

"2011-12-25"

Upvotes: 2

Seb
Seb

Reputation: 5497

Hi are you intending to achieve this?

 new <- as.POSIXlt("2011-12-25 15:47:21")
 format(new, format="%d/%m/%Y")

Upvotes: 6

Related Questions