[R] Convert time format to character

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Tue Nov 4 03:38:05 CET 2014


I think the OP is being vague by saying "time format" (wouldn't a small reproducible example from the OP help right now)... if that is really another character string laid out as described then you might have difficulty with time zones for some values by converting to POSIXct first. If so, you can avoid the double conversion by using sub:

timestr <- "2014-08-13 00:30:00"
result <- sub("^(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})$", "\\1\\2\\3\\4\\5\\6", timestr )

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On November 3, 2014 9:01:55 PM EST, Peter Alspach <Peter.Alspach at plantandfood.co.nz> wrote:
>Tena koe
>
>For example:
>
>myTime <- Sys.time()
>myTime
>[1] "2014-11-04 14:58:10 NZDT"
>format(myTime, '%Y%m%d:%H%M%S')
>[1] "20141104:145810"
>
>My information in ?strptime.
>
>HTH ....
>
>Peter Alspach
>
>-----Original Message-----
>From: r-help-bounces at r-project.org
>[mailto:r-help-bounces at r-project.org] On Behalf Of TJUN KIAT TEO
>Sent: Tuesday, 4 November 2014 11:10 a.m.
>To: r-help at r-project.org
>Subject: [R] Convert time format to character
>
>Suppose I have a object in time format
>
>2014-08-13 00:30:00
>
>I want to convert it to a character string 20140813003000
>
>Is is possible ?
>
>Tjun Kiat
> 		 	   		  
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>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.
>The contents of this e-mail are confidential and may be
>...{{dropped:14}}
>
>______________________________________________
>R-help at r-project.org mailing list
>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