HJain
HJain

Reputation: 145

Extract DateTime from a string in R

One of the columns of my data frame have DateTime and some string characters together. LIke:

"<13>1 2018-04-18T10:29:00.581243+10:00 KOI-QWE-HUJ vmon 2318 - -  Some Description..."

I wish to extract only the DateTime part from it so that I have something like this:

"2018-04-18 10:29:00.581243"

I have tried the below:

as.Date(strptime("<13>1 2018-04-18T10:29:00.581243+10:00 KOI-QWE-HUJ vmon 2318 - - Some Description...", "Date: %Y-%m-%d")) But, this returns NA.

Can anyone please rectify this for me. Thanks in advance.

EDIT: I tried the below to get the Date and Time separately. This works but I need to get them together in a column:

To extract Date:

as.Date(str_extract(x, "[0-9]{4}-[0-9]{2}-[0-9]{2}"), format="%Y-%m-%d")
> [1] "2018-04-18"

To extract Time:

str_extract(x, "[0-9]{2}:[0-9]{2}:[0-9]{2}")
> [1] "10:29:00"

Upvotes: 3

Views: 1963

Answers (3)

sotnik
sotnik

Reputation: 119

I have a string like

x="extracted date: Dec 14, 2020 10:01PM ET"

The desired output is:

"2020-12-14 22:01 ET"

I have tried various functions from similar answers to similar questions without success

Thank you

Upvotes: -1

Onyambu
Onyambu

Reputation: 79228

anytime::anytime(sub(".*?\\s(.*?)\\+.*","\\1",a),tz = "UTC",T)
[1] "2018-04-18 10:29:00 UTC"

Upvotes: 0

Calum You
Calum You

Reputation: 15072

You are right that you should extract the character form of the datetime first. Here is a method that works with that format. It's just using a regular expression and matching 4 digits, then groups of two digits separated by -, T and : where appropriate. You can then use lubridate::ymd_hms as an alternative to as.Date, since it's a good Swiss army knife at different date formats.

library(stringr)
library(lubridate)
string <- "<13>1 2018-04-18T10:29:00.581243+10:00 KOI-QWE-HUJ vmon 2318 - -  Some Description..."
string %>%
  str_extract("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}") %>%
  ymd_hms()
#> [1] "2018-04-18 10:29:00 UTC"

Created on 2018-05-02 by the reprex package (v0.2.0).

Upvotes: 4

Related Questions