[R] Query about use of format in strptime

Jim Lemon drjimlemon at gmail.com
Mon Apr 11 09:47:55 CEST 2016

Hi Stefano,
As the help page says:

"The default for the format methods is "%Y-%m-%d %H:%M:%S" if any
element has a time component which is not midnight, and "%Y-%m-%d"
otherwise. This is because when the result is printed, it uses the
default format. If you want a specified output representation:

format(strptime(init_day, format="%Y-%m-%d-%H-%M"),"%Y-%M-%d %H:%M")
[1] "2015-30-24 00:30"

For the "midnight" case:

format(strptime(init_day, format="%Y-%m-%d-%H-%M"),"%Y-%m-%d %H:%M")
[1] "2015-02-24 00:00"


On Mon, Apr 11, 2016 at 5:22 PM, Stefano Sofia
<stefano.sofia at regione.marche.it> wrote:
> Dear R-list users,
> I need to use strptime because I have to deal with date with hours and minutes.
> I read the manual for strptime and I also looked at many examples, but when I try to apply it to my code, I always encounter some problems.
> I try to change the default format, with no success. Why? How can I change the format?
> 1.
> init_day <- as.factor("2015-02-24-00-30")
> strptime(init_day, format="%Y-%m-%d-%H-%M")
> [1] "2015-02-24 00:30:00"
> It works, but why also seconds are shown if in format seconds are not specified?
> 2.
> init_day <- as.factor("2015-02-24-0-00")
> strptime(init_day, format="%Y-%m-%d-%H-%M")
> [1] "2015-02-24"
> Again, the specified format is not applied. Why?
> Thank you for your attention and your help
> Stefano
> ________________________________
> 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. 6 della DGR n. 1394/2008 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]]
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list