[R] managing dates in POSIXct format at 00

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Fri Nov 14 19:13:51 CET 2025


On 2025-11-14 12:47 p.m., Iris Simmons wrote:
> R does this for convenience when printing values. But it's not always
> convenient when writing to a file. Instead, you could use strftime to
> convert to your desired character format before writing:
> 
> mydf$data_POSIX <- strftime(mydf$data_POSIX, "%Y-%m-%d %H:%M:%OS3")
> 
> you can optionally change the 3 to a number between 0 and 6. If you choose
> 0, it's easier to just use %S instead.

That can happen automatically if you set an as.character() method:

   as.character.POSIXct <- function(x, ...) strftime(x, "%Y-%m-%d 
%H:%M:%OS3")

(By default, there's a POSIXt method, but no POSIXct method.)

Time zone issues still make mydf2 different from mydf, but you won't get 
NAs.

Duncan Murdoch



More information about the R-help mailing list