haimen
haimen

Reputation: 2015

character to date time variable in R

I have a character variable,named date with values like "2015-10-17T02:00:00" I want to convert this to date and time. I used the following,

as.POSIXct(format(date, format = "%Y-%m-%d %H:%M:%S"))

But I am getting, "2015-10-17 PDT" I am losing the time factor here. Can anybody help me what mistake I am doing here?

Upvotes: 0

Views: 1397

Answers (1)

Pierre L
Pierre L

Reputation: 28441

The format should reflect the exact layout of the data. Your data has "year month day" separated by hyphen, then "T", then "hour minutes and seconds" separated by colon. Your format should show that exactly.

as.POSIXct("2015-10-17T02:00:00", format = "%Y-%m-%dT%H:%M:%S")
#[1] "2015-10-17 02:00:00 EDT"

edit

The format "%Y-%m-%d" can be shortened to "%F", and "%H:%M:%S" can be replaced with "%T".

Upvotes: 1

Related Questions