finraj
finraj

Reputation: 13

Character string with timezone convert to date

I am trying to convert a vector of dates that I read from a csv file using read.table. These were read as a vector of character strings. I am trying to convert it to a date vector using as_date.

The date vector has elements of the below type

dateString
"Wed Dec 11 00:00:00 ICT 2013"

On trying to convert using the below command,

as.Date(dateString,"%a %b %e %H:%M:%S %Z %Y")
Error in strptime(x, format, tz = "GMT") : 
  use of %Z for input is not supported

What would be the right format to use in strptime? or in as.Date?

Upvotes: 1

Views: 296

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368241

Just use the anytime() function from the anytime package:

R> anytime::anytime("Wed Dec 11 00:00:00 ICT 2013")
[1] "2013-12-11 CST"
R> 

There is also an utctime() variant to not impose your local time, and much. By now we also had a number of questions here so just search.

And if you want a date, it works the same way:

R> anytime::anydate("Wed Dec 11 00:00:00 ICT 2013")
[1] "2013-12-11"
R> 

Upvotes: 3

Related Questions