0

I would like to transform January 2, 2018 at 02:55PM to datetime format . I use the package anytime. I added a new format addFormats("%B %d, %Y at %I:%M%p") but it doesn't work.

anytime(data$Date) returns "2018-01-02 CET". I lose the hour..

Gowachin
  • 1,251
  • 2
  • 9
  • 17

1 Answers1

0

I assume some format for creating the time string as follow h <- "January 2, 2018 at 02:55PM"

The key I found was a modification of the string, in removing "at " and "PM". I've done it in two step, but I guess someone could do this in just one step. I did not put format like you.

h <- "January 2, 2018 at 02:55PM"
h ; anytime(h)
#[1] "January 2, 2018 at 02:55PM"
#[1] "2018-01-02 CET"

h <- gsub("at ","", h,)
h ; anytime(h)
#[1] "January 2, 2018 02:55PM"
#[1] "2018-01-02 CET"

h <- gsub("PM","", h,)
h ; anytime(h)
#[1] "January 2, 2018 02:55"
#[1] "2018-01-02 02:55:00 CET"

Note that whitout seconds, it assume the hour with hour:minute:00.

EDIT :

There is one regex to remove both patterns in one time

h <- "January 2, 2018 at 02:55PM"
h <- gsub("at |PM","",h)
anytime(h)
#[1] "2018-01-02 02:55:00 CET"
Community
  • 1
  • 1
Gowachin
  • 1,251
  • 2
  • 9
  • 17