Search code examples
rdatedatetimetimestampposixct

Change datetime e.g. 01JAN2020:13:04:10.00000 in r


I haven't been able to find another question - but I feel like there is one out there, so sorry if I'm asking a duplicate question!

I have a date time column in the following format (character):

date <- "02OCT2019:15:04:34.000000"

I've tried to convert the column to a date with

new_date <- as.POSIXct(date, "%d%b%Y:%H:%M:%S%OS")

But it doesn't seem to work. I feel like my problem lies in the month part because it's written in caps. I tried substringing just the date (%d%b%Y) and using as.POSIXct on that, getting the same error.

I don't know if it's relevant, but my locale is

Sys.getlocale()
[1]"LC_COLLATE=Danish_Denmark.1252;LC_CTYPE=Danish_Denmark.1252;LC_MONETARY=Danish_Denmark.1252;LC_NUMERIC=C;LC_TIME=Danish_Denmark.1252"

And the month format used is not Danish, but English (hence OCT, in Danish it would be OKT).

Hope some of you can help me out!


Solution

  • library(lubridate)
    
    date <- "02OCT2019:15:04:34.000000"
    
    dmy_hms(date)
    
    # [1] "2019-10-02 15:04:34 UTC"