I am trying to extract the date and time out of the following but am having some difficulties in the format. I have the following object "2024-10-10T12:57:26.61Z"
and am trying to get it into the format of "2024-10-10 12:57:26"
. This is in character class.
I've currently tried as.POSIXct()
but that only returns "2024-10-10 BST"
without the time which is need here. My guess is that the issue is mostly to do with class of the object
You need to specify the format, using T
to separate the date from the time is not recognised by default (though some add-on packages do). Also the time zone by default becomes the local time zone:
> as.POSIXct("2024-10-10T12:57:26.61Z", format="%Y-%m-%dT%H:%M:%S")
[1] "2024-10-10 12:57:26 CDT"
>
This converts it into 'compact' form, a single (double) value. You can also use 'long' form after which you can access the components:
> x <- as.POSIXlt("2024-10-10T12:57:26.61Z", format="%Y-%m-%dT%H:%M:%S")
> x
[1] "2024-10-10 12:57:26 CDT"
> unlist(x)
sec min hour mday mon year wday yday isdst zone gmtoff
"26" "57" "12" "10" "9" "124" "4" "283" "1" "CDT" NA
>
The components are described on the help page, in particular year is the actual year minus 1900, and months start at zero not one.
Once parsed as a proper (date)time object, you can format to your liking. The format you show is the default format (and a good ISO standard).