Extract time (HMS) from lubridate date time object? - r

I have the following datetime:
t <- "2018-05-01 23:02:50 UTC"
I want to split it to time and date.
When I apply date(t) I get the date part.
But when I use lubridate's hms, parse_date_time and other functions to do this in "HMS" order I get NA.
I have checked other answers here on SOF but for some reason it gives me NA.
Please advise how to extract it.
I want to understand why:
strftime(t, format="%H:%M:%S")
will do the job but what I am missing in lubridate::hms or parse_date_time?

Is this what you were looking for? It can now be done more simply with hms::as_hms.
> library(lubridate)
> library(hms)
> as_hms(ymd_hms("2018-05-01 23:02:50 UTC"))
23:02:50
> t <- "2018-05-01 23:02:50 UTC"
> as_hms(ymd_hms(t))
23:02:50

My solution is to install library(anytime):
date <- anytime::anydate(t)
time <- strftime(t, format="%H:%M:%S")

What you are missing in lubridate's hms() is that it expects "a character vector of hour minute second triples" as an argument. There's no provision for handling a string which also contains date info. Hence, the output of Sys.Date() or lubridate::now() doesn't work as input to lubridate::hms().
In case you want a tidyverse solution, here's one:
library(tidyverse)
library(lubridate)
now()
#> [1] "2018-08-13 16:41:31 BST"
get_time <- function(time = now()) {
time %>%
str_split(" ") %>%
map_chr(2) %>%
hms()
}
get_time()
#> [1] "16H 41M 31S"
get_time("2018-05-01 23:02:50 UTC")
#> [1] "23H 2M 50S"
Created on 2018-08-13 by the reprex package (v0.2.0).

Something like this?
library(hms)
t <- "2018-05-01 23:02:50 UTC"
unlist(strsplit(t," "))[2]%>%hms::parse_hms()

Here is a solution without including just another package (hms on top of lubridate):
t <- "2018-05-01 23:02:50 UTC"
sprintf("%02d:%02d:%02d", hour(t), minute(t), second(t))
"23:02:50"

The fucntion in Lubridate package exists, it is called "hour()", here the official guide:
https://lubridate.tidyverse.org/reference/hour
t <- "2018-05-01 23:02:50 UTC"
lubridate::hour(t)

Posting a solution that I have used to extract either ymd or the hms independently after a lubridate conversion. I noticed OP mentioned wanting to separate both time and date so for future SO users who may find themselves here I included the regex's used for both below. assuming df contains a column called date where time is formated ymd_hms like so "2018-05-01 23:02:50 UTC":
library(stringr)
df <- df %>%
mutate(
time = str_extract(date, "[0-9]{2}:[0-9]{2}:[0-9]{2}") #to extract hms time
day = str_extract(date, "[0-9]{4}-[0-9]{2}-[0-9]{2}") #to extract ymd time
)

Related

How do remove the UTC from the date using R

I am trying to remove the utc from this data and just keep it in single quotes this is the function i am using in R.
date.start = as.Date(Sys.Date())
But i am getting this result
I guess date.start is Sys.time() therefore do:
date.start = as.Date(Sys.time())
Sys.Date()
Sys.time()
Sys.timezone()
as.Date(Sys.time())
Output:
> Sys.Date()
[1] "2021-08-17"
> Sys.time()
[1] "2021-08-17 09:14:33 CEST"
> Sys.timezone()
[1] "Europe/Berlin"
> as.Date(Sys.time())
[1] "2021-08-17"
I think that the timezone 'UTC' is being posited there by your system settings. I believe that generating the system date with lubridate might sidestep the issue within R:
date.start = lubridate::today(tzone = "")
Use sub:
sub(" UTC", "", date)
[1] "2021-08-17" "2020-12-12"
Test data:
date <- c("2021-08-17 UTC", "2020-12-12 UTC")
Try using different time formats when getting data.
format(Sys.time(),"%d-%m-%y")
For better understanding you can read rbloggers article on Date Formats in R here:
https://www.r-bloggers.com/2013/08/date-formats-in-r/
I'm not sure why you want to remove it. That would help. Another answer showed you how to convert it to a string.
But you'll want it in date format to do something like seq(Sys.Date(), Sys.Date() + 24, by = 'day').
If the reason you want it in a particular time zone is to to join data set at midnight, you should use lubridate's force_tz ala force_tz(Sys.Date(), 'America/Chicago'). Be careful, here because it the timezone will change as needed due to daylight savings. That's why it's usually better to stick with UTC anyways.
Otherwise, as the other poster mentioned, just convert to string and format it ala format(Sys.Date(),"%Y-%m-%d").

Formatting Date Strings in R

I have two columns of differently formatted date strings that I need to make the same format,
the first is in the form:
vt_dev_date = "6/20/2016 7:45"
the second is in the form
vt_other = "2016-06-14 20:21:29.0"
If could get them both in the same form down to the minute that would be great. I have tried
strptime(vt_dev_date,format = "%Y-%m-%d %H:%M")
strptime(vt_other,"%Y-%m-%d %H:%M")
and for the second one, it works and I get
"2016-06-14 20:21:00 EDT"
But for the first string, it seems that because the month and hour are not padded with zeros, none of the formating tricks will work, becuase if I try
test_string <- "06/20/2016 07:45"
strptime(test_string,format = "%m/%d/%Y %H:%M")
[1] "2016-06-20 07:45:00 EDT"
It works, but I dont think going through every row in the column and padding each date is a great option. Any help would be appreciated.
Thanks,
josh
How about using lubridate , as follows :
library(lubridate)
x <- c("6/20/2016 7:45","2016-06-14 20:21:29.0")
> x
[1] "6/20/2016 7:45" "2016-06-14 20:21:29.0"
> parse_date_time(x, orders = c("mdy hm", "ymd hms"))
[1] "2016-06-20 07:45:00 UTC" "2016-06-14 20:21:29 UTC"
>

Numeric to DateTime format in R

I would like to know what is the best way to convert 201509122150 (numeric) to Date class within YYYY-MM-DD hh:mm format.
E.g.
x <- 201509122150
as.POSIXct(as.character(x), format="%Y%m%d%H%M")
# [1] "2015-09-12 21:50:00 CEST"
This can be easily done with lubridate
library(lubridate)
ymd_hm(x)
#[1] "2015-09-12 21:50:00 UTC"
data
x <- 201509122150

Parse date using lubridate in R

I want to parse date format like "Apr-13", "May-14", which I want to parse it to POSIX time "2013-04-01 UTC", "2014-05-01 UTC". I tried using lubridate package in R:
parse_date_time("Apr-13", "my")
parse_date_time("Apr-13", "by")
parse_date_time("Apr-13", "%b-%y")
parse_date_time("Apr-13", "%m-%y")
fast_strptime("Apr-13", "%b-%y")
But all of them failed to parse, is it possible to parse this using lubridate? Any other package is also welcome.
Try this example:
library(lubridate)
x <- c("Apr-13", "May-14")
#add DD as 01
x <- paste("01",x,sep="-")
#result
dmy(x)
#output
#[1] "2013-04-01 UTC" "2014-05-01 UTC"

Converting non-standard dates in r

My date information is a string in the following format: 3/12/1956 0:00:00
I have tried converting it using DOB<-as.Date(DOB, "%d/%m/%y %H:%M:%S")
I am trying to convert it for the purpose of then applying the age_calc function in eeptools package.
Is there some other way to change a non standard format into a date. Damn Aussie dates!
You can try lubridate as an alternative
library(lubridate)
DOB <- '3/12/1956 0:00:00'
mdy_hms(DOB)
#[1] "1956-03-12 UTC"
It can also take multiple formats
DOB <- c('3/12/1956 0:00:00', '3.12/1956 0.00/00')
mdy_hms(DOB)
#[1] "1956-03-12 UTC" "1956-03-12 UTC"
Or as #Richard Scriven commented,
as.Date(DOB, "%d/%m/%Y %H:%M:%S")
#[1] "1956-12-03"

Resources