1

I have some dates and times in a .csv I'm bringing into R.

I then set them as a POSIXct object and specify the format. Making this change changes the format of how the date-time is displayed. I can then force it back to the original format by using the format function but then the object becomes a character and is no longer POSIXct. Is there a way to tell POSIXct what format for the date I want or to set a global default for how dates and date-times are displayed in R? I'd love to be able to just set my date-time once and have it be how I want it to be.

Here's some sample code:

# some data
df = data.frame(other_var = c("something", "else", "here"),
                date_time = c("6/5/2020 00:10:10", "6/6/2020 00:10:11", "6/7/2020 01:10:11"))

# set as.POSIXct
df$date_time = as.POSIXct(df$date_time, format = "%m/%d/%Y %H:%M:%S", tz="UTC")
# check structure
str(df$date_time)

# make pretty
df$date_time = format(df$date_time, format = "%m/%d/%Y %H:%M:%S", tz="UTC")
# check structure
str(df$date_time) #is character! :(
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Jade131621
  • 316
  • 1
  • 13
  • Try https://stackoverflow.com/a/69468780/3358272. The notion is that you assign a leading class to your otherwise `POSIXt` data, and define your own `format` for that vector/column. It is imperfect, though, in that doing anything to the vector (e.g., `vec + 1` or `1 + vec` will drop the special class and resort to the original formatting. – r2evans Feb 03 '23 at 20:47
  • 2
    I was going to suggest hacking the `print.POSIXct` method to do whatever you wanted, but the linked duplicate is better (except for the limitations pointed out by @r2evans). (If it doesn't satisfy your needs, edit your question to explain why the previous answer isn't sufficient and we'll vote to reopen.) – Ben Bolker Feb 03 '23 at 20:50
  • 1
    You can't set the global default for how dates and date-times are displayed in R. Under the hood, dates are stored as numeric and are converted to character when they are printed. So if you have a strong preference for the format you want, you need to specifically format the values yourself. Same goes for most values. There's no way to change the default way boolean values are formatted or floating numbers are formatted; that's all built in. – MrFlick Feb 03 '23 at 20:53
  • ??? but couldn't you hack the `print.Date` or `print.POSIXct` methods (i.e. put modified versions of the original in your global workspace? e.g. `print.POSIXct <- function (x, tz = "", usetz = TRUE, max = NULL, ...) { invisible(cat("no way\n")) }; Sys.time()` – Ben Bolker Feb 03 '23 at 21:07
  • 2
    Try this: `format.POSIXct <- function(x, tz = "", usetz = FALSE, ...) structure(format.POSIXlt(as.POSIXlt(x, tz), format = "%m/%d/%Y %H:%M:%OS", usetz, ...), names = names(x)); print.POSIXct <- function(x, tz = "", usetz = TRUE, ...) print(format.POSIXct(x, tz = tz, usetz = usetz, ...));`. After that, `Sys.time()` is printed correctly but `data.frame(a=Sys.time())` is not, for that you need to nest a bit deeper. ***It's not easy***, and it's fragile. I understand the frustration but have found no way around it. – r2evans Feb 03 '23 at 21:15

0 Answers0