Re: managing dates in POSIXct format at 00

Iris Simmons <[email protected]>
Newsgroups gmane.comp.lang.r.general
Message-ID <CADNULg-PZES9D4mP42i4qihK3f0rVZb3PU+SnZ-r59QS+kfaZQ@mail.gmail.com>
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.


On Fri, Nov 14, 2025, 12:30 Stefano Sofia via R-help <[email protected]>
wrote:

> Dear R-list users,
>
> this question will seem silly to most of you, but I really got mad trying
> to find a solution with no success, and therefore I decided to write here.
>
>
> Suppose I create the following data frame:
>
>
> mydf <- data.frame(date = c("2007-11-19 00:00:00.000", "2007-11-19
> 06:00:00.000", "2007-11-19 12:00:00.000", "2007-11-19 18:00:00.000",
> "2007-11-20 00:00:00.000"), value = rep(10, 5))
>
> mydf$data_POSIX <- as.POSIXct(mydf$date, format = "%Y-%m-%d %H:%M:%S",
> tz="Etc/GMT-1")
>
> max(mydf$data_POSIX)
>
>
> gives, obviously,
>
>
> [1] "2007-11-20 +01"
>
>
> Now if I export this data frame in a txt file
>
>
> write.table(mydf, file="mydf.txt", sep = ",", dec=".", row.names=FALSE,
> col.names = TRUE, quote=FALSE)
>
>
> the file is stored like
>
>
> date,value,data_POSIX
> 2007-11-19 00:00:00.000,10,2007-11-19
> 2007-11-19 06:00:00.000,10,2007-11-19 06:00:00
> 2007-11-19 12:00:00.000,10,2007-11-19 12:00:00
> 2007-11-19 18:00:00.000,10,2007-11-19 18:00:00
> 2007-11-20 00:00:00.000,10,2007-11-20
>
> Hours, minutes and seconds at 00 are not shown anymore.
> When I read back this file into R:
>
> mydf2 <- read.table(file="mydf.txt", header = TRUE, sep=",", dec = ".")
>
> mydf2$data_POSIX <- as.POSIXct(mydf2$data_POSIX, format = "%Y-%m-%d
> %H:%M:%S", tz="Etc/GMT-1")
>
> I get
>                      date value          data_POSIX
> 1 2007-11-19 00:00:00.000    10                <NA>
> 2 2007-11-19 06:00:00.000    10 2007-11-19 06:00:00
> 3 2007-11-19 12:00:00.000    10 2007-11-19 12:00:00
> 4 2007-11-19 18:00:00.000    10 2007-11-19 18:00:00
> 5 2007-11-20 00:00:00.000    10                <NA>
>
> and
>
> max(mydf2$data_POSIX)
>
>
> does not work anymore.
>
> Where is my mistake? Is there a solution?
>
> Thank you for your attention and your help
>
> Stefano
>
>
>
>
>
>          (oo)
> --oOO--( )--OOo--------------------------------------
> Stefano Sofia MSc, PhD
> Civil Protection Department - Marche Region - Italy
> Meteo Section
> Snow Section
> Via Colle Ameno 5
> 60126 Torrette di Ancona, Ancona (AN)
> Uff: +39 071 806 7743
> E-mail: [email protected]
> ---Oo---------oO----------------------------------------
>
> ________________________________
>
> AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere
> informazioni confidenziali, pertanto è destinato solo a persone autorizzate
> alla ricezione. I messaggi di posta elettronica per i client di Regione
> Marche possono contenere informazioni confidenziali e con privilegi legali.
> Se non si è il destinatario specificato, non leggere, copiare, inoltrare o
> archiviare questo messaggio. Se si è ricevuto questo messaggio per errore,
> inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio
> computer. Ai sensi dell'art. Ai sensi dell'art. 2.4 dell'allegato 1 alla
> DGR n. 74/2021, si segnala che, in caso di necessità ed urgenza, la
> risposta al presente messaggio di posta elettronica può essere visionata da
> persone estranee al destinatario.
> IMPORTANT NOTICE: This e-mail message is intended to be received only by
> persons entitled to receive the confidential information it may contain.
> E-mail messages to clients of Regione Marche may contain information that
> is confidential and legally privileged. Please do not read, copy, forward,
> or store this message unless you are an intended recipient of it. If you
> have received this message in error, please forward it to the sender and
> delete it completely from your computer system.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.