Skip to content

changing date by +/- days

4 messages · chuck.01, Jeff Newmiller, arun

#
Hi, 
Let say I have a date variable like this:

structure(1243792800, class = c("POSIXct", "POSIXt"), tzone = "GMT")
[1] "2009-05-31 18:00:00 GMT"

How can I make it 10 days prior, so:
[1] "2009-05-21 18:00:00 GMT"

I'm randomly selecting dates from a list  and want a  second value a set #
of days either before or after. 

Thanks ahead of time. 



--
View this message in context: http://r.789695.n4.nabble.com/changing-date-by-days-tp4647803.html
Sent from the R help mailing list archive at Nabble.com.
#
got it, sorry!

structure(1243792800, class = c("POSIXct", "POSIXt"), tzone =
"GMT")-(60*60*24*10)




chuck.01 wrote
--
View this message in context: http://r.789695.n4.nabble.com/changing-date-by-days-tp4647803p4647804.html
Sent from the R help mailing list archive at Nabble.com.
#
I try to use difftimes as much as possible.  For example,

oneday <- as.difftime( 24, units="hours" )
sometime - 10 * oneday

You might also find the lubridate package useful.
---------------------------------------------------------------------------
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.
"chuck.01" <CharlieTheBrown77 at gmail.com> wrote:

            
#
Hi,
Try this:
dat1<-structure(1243792800, class = c("POSIXct", "POSIXt"), tzone = "GMT")
?dat2<-as.POSIXlt(dat1)
?dat2$mday<-dat2$mday-10
?dat2
#[1] "2009-05-21 18:00:00 GMT"
A.K.





----- Original Message -----
From: chuck.01 <CharlieTheBrown77 at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Monday, October 29, 2012 8:37 PM
Subject: [R] changing date by +/- days

Hi, 
Let say I have a date variable like this:

structure(1243792800, class = c("POSIXct", "POSIXt"), tzone = "GMT")
[1] "2009-05-31 18:00:00 GMT"

How can I make it 10 days prior, so:
[1] "2009-05-21 18:00:00 GMT"

I'm randomly selecting dates from a list? and want a? second value a set #
of days either before or after. 

Thanks ahead of time. 



--
View this message in context: http://r.789695.n4.nabble.com/changing-date-by-days-tp4647803.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.