rdatetimedatasetposixct

Read date variable separated by points


My dataset has a variable with date and time, where the days, months and years are separated by points instead of bars. For example:

example <- "13.04.2021 12:00:00"

When I convert it to POSIXct it returns NAs, probably due to the points. Is there any way to change the points into bars so that the function recognizes the variable as date?


Solution

  • Either of these will work:

    as.POSIXct(example, format = "%d.%m.%Y %H:%M:%S")
    lubridate::as_datetime(example, format = "%d.%m.%Y %H:%M:%S")
    

    For more information, see:

    https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/as.POSIX*

    or

    https://lubridate.tidyverse.org/reference/as_date.html