Skip to content

Month Difference

5 messages · Rantony, PIKAL Petr, Akkara, Antony (GE Energy, Non-GE) +2 more

#
Hi,

i need a small help.  

a<- as.Date("2012-01-01")
b<- as.Date("2012-12-01")
------------------------------------
My requirement what is, i need to get the month difference of these two
dates.
Can any one help me please !
- Antony



--
View this message in context: http://r.789695.n4.nabble.com/Month-Difference-tp4642483.html
Sent from the R help mailing list archive at Nabble.com.
#
One possibility

as.numeric(format(b, "%m"))-as.numeric(format(a, "%m"))

Regards
Petr
#
Yes. Its working fine. We need to convert to "as.Date" thatz all.
Thank you Petr.

-----Original Message-----
From: PIKAL Petr [mailto:petr.pikal at precheza.cz] 
Sent: Friday, September 07, 2012 3:42 PM
To: Akkara, Antony (GE Energy, Non-GE); r-help at r-project.org
Subject: RE: [R] Month Difference

One possibility

as.numeric(format(b, "%m"))-as.numeric(format(a, "%m"))

Regards
Petr
#
On 07-09-2012, at 10:26, Rantony wrote:

            
One way is:

library(zoo)
(as.yearmon(b)-as.yearmon(a))*12

or

a.lt <- as.POSIXlt(a)
b.lt <- as.POSIXlt(b)
b.lt$mon-a.lt$mon

Searching in Google with  "R date difference months" gives quite a few interesting results.

Berend
#
Le 07/09/12 10:26, Rantony a ?crit :
Here is a solution from here:
http://129.175.106.17/epc/conservation/Girondot/Publications/Blog_r/Entrees/2011/9/18_Nombre_de_mois_entre_deux_dates.html

num.months    <-function ( x , y )
+ {
+         x<-as.Date( x )
+         y<-as.Date( y )
+         seeq<-length(seq(from=x , to=y , by="months"))-1
+         seeq
+ }

x <- c("1981-12-01")
y <- c("1990-05-01")

indicemonth <- num.months ( x , y )

Sincerely

Marc Girondot