Eliel Epelbaum
Eliel Epelbaum

Reputation: 69

Converting date/time containing month as string to date format

I have a date/time vector where the month appears as a string format (e.g, Jun, May, Jul).

I would like to convert the current factor date variable to a date format. The date currently appears as follow:

"01/Jun/18 1:22 AM", "12/May/18 3:14 AM", "23/Jul/18 11:47 AM" 

Upvotes: 2

Views: 156

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388807

We can use as.Date or as.POSIXct based on the output you want

as.Date(x, "%d/%b/%y %H:%M")
#[1] "2018-06-01" "2018-05-12" "2018-07-23"

as.POSIXct(x, format = "%d/%b/%y %H:%M")
#[1] "2018-06-01 01:22:00 GMT" "2018-05-12 03:14:00 GMT" "2018-07-23 11:47:00 GMT"

Read ?strptime for more information on the details of the format.

data

x <- c("01/Jun/18 1:22 AM", "12/May/18 3:14 AM", "23/Jul/18 11:47 AM")

Upvotes: 1

Related Questions