How do remove the UTC from the date using R

3k Views Asked by At

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 enter image description here

5

There are 5 best solutions below

0
Chris Ruehlemann On

Use sub:

sub(" UTC", "", date)
[1] "2021-08-17" "2020-12-12"

Test data:

date <- c("2021-08-17 UTC", "2020-12-12 UTC")
0
rkabuk On

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/

0
ideamotor On

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").

0
navona On

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 = "")
0
TarJae On

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"