简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM