Page 1 of 2

Año(s), mes(es) y dia(s) transcurridos entre dos fechas

Posted: Fri Mar 15, 2013 4:08 am
by tave2009
Hola Amigos:

En esta oportunidad les acerco una función para obtener: año(s), mes(es) y día(s), entre dos fechas ;)

:arrow: CalculaAMD( <FechaInicial>, <FechaFinal>) ==> cResultado

He buscado por los foros y no encontré algo que funcionara correctamente, por lo tanto decidí hacer mi propia función.
Espero sea de utilidad para quien quiera saber la edad o el tiempo transcurrido entre dos fechas.
El código fuente puede contener errores. Lo he 'testeado' sin novedad, pero ...
Saludos.
Walter.

Google Translation:

Hello Friends:

This time I approached a role for the year (s), month (s) and day (s) between two dates;)

: arrow: CalculaAMD (<FechaInicial>, <FechaFinal>) ==> cResultado

I've searched the forums and did not find anything that would work properly, so I decided to make my own function.
Hope this helps for those who want to know the age or the time between two dates.
The source code may contain errors. I've 'tested' without incident, but ...
Greetings.
Walter.

Re: Año(s), mes(es) y dia(s) transcurridos entre dos fechas

Posted: Fri Mar 15, 2013 4:32 am
by Rathinagiri
Thanks. It will be really useful.

Re: Año(s), mes(es) y dia(s) transcurridos entre dos fechas

Posted: Fri Mar 15, 2013 4:53 am
by esgici
Hola Walter

It's nice you write in two languages ;)
tave2009 wrote:This time I approached a role for the year (s), month (s) and day (s) between two dates;)
...
I've searched the forums and did not find anything that would work properly,
Thanks to sharing :)

viewtopic.php?f=9&t=7&p=13

Saludos

Re: Año(s), mes(es) y dia(s) transcurridos entre dos fechas

Posted: Fri Mar 15, 2013 6:12 am
by Rathinagiri
Hi Esgici,

I had made an assumption that months have 30 days, where as Walter is not assuming things and count for actual number of days, months and years.

Re: Año(s), mes(es) y dia(s) transcurridos entre dos fechas

Posted: Fri Mar 15, 2013 9:15 am
by esgici
rathinagiri wrote:Hi Esgici,

I had made an assumption that months have 30 days, where as Walter is not assuming things and count for actual number of days, months and years.
Thanks to you both :)

Viva HMG :D

Re: Año(s), mes(es) y dia(s) transcurridos entre dos fechas

Posted: Fri Mar 15, 2013 1:51 pm
by Pablo César
rathinagiri wrote:where as Walter is not assuming things and count for actual number of days, months and years.
Yeah, this is a good differance !

Thank you Walter for sharing with us, I had appointed to your post by coincidence for same matter in other forum.

Muchas gracias Walter y felicitaciones !

One question Walter: Not all february has 28 days, did you treat that ?

In Spanish: Walter, no todos los meses de febrero tienen 28 dias, has tratado eso en tu rutina de código ?

Re: Año(s), mes(es) y dia(s) transcurridos entre dos fechas

Posted: Fri Mar 15, 2013 2:15 pm
by Rathinagiri
Yes, he has.

Re: Año(s), mes(es) y dia(s) transcurridos entre dos fechas

Posted: Fri Mar 15, 2013 3:04 pm
by Pablo César
Ahhh yes amigio Rathinagiri, Walter has treated this in:

if nMesActual == 2 so it is not using vetor...

Thanks !

Re: Año(s), mes(es) y dia(s) transcurridos entre dos fechas

Posted: Fri Mar 15, 2013 4:31 pm
by Leopoldo Blancas
Hola a todos...

Yo hice esta rutina para calcular la Edad o antigüedad entre dos fechas... Espero les sirva.
*------------------------------------------------------------------------------------------------------------------------------------------------
Hi all ...

I did this routine to calculate the age or seniority between two dates ... I hope they can.

Code: Select all

FUNCTION CalculaEdad(cFechaNaci,cFechaHoy)
LOCAL FechaNaci:= CtoD(cFechaNaci)
LOCAL nAnoNaci := YEAR(FechaNaci)
LOCAL nMesNaci := MONTH(FechaNaci)
LOCAL nDiaNaci := DAY(FechaNaci)
LOCAL nAnoHoy       
LOCAL nMesHoy        
LOCAL nDiaHoy        
LOCAL nDifAnos  := 0
LOCAL nDifMeses := 0
LOCAL nDifDias  := 0
LOCAL aEdad

LOCAL lAnosMenos := .F.

LOCAL aMesesDias := {}
LOCAL aBisiestos := {1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008,2012,2016,2020,2024,2028,2032,2036,2040,2044,2048,2052,2056,2060,2064,2068,2076,2080,2084,2088,2092,2096,2100}
LOCAL lBisiesto := .F.


IF PCOUNT()=0
   MsgBox("Falta un parametro String Tipo Fecha '01/01/1900'")
   RETURN NIL
ELSEIF PCOUNT()=1
   nAnoHoy := YEAR(DATE())
   nMesHoy := MONTH(DATE())
   nDiaHoy := DAY(DATE())
ELSEIF PCOUNT()=2  
   nAnoHoy := YEAR(CtoD(cFechaHoy))
   nMesHoy := MONTH(CtoD(cFechaHoy))
   nDiaHoy := DAY(CtoD(cFechaHoy))
ENDIF

IF PosiArray(aBisiestos,nAnoNaci) != 0
   lBisiesto := .T.
ENDIF

AADD(aMesesDias,{1,'ENERO',31})
AADD(aMesesDias,{2,'FECBRERO',IIF(lBisiesto,29,28)})
AADD(aMesesDias,{3,'MARZO',31})
AADD(aMesesDias,{4,'aBRIL',30})
AADD(aMesesDias,{5,'MAYO',31})
AADD(aMesesDias,{6,'JINIO',30})
AADD(aMesesDias,{7,'JULIO',31})
AADD(aMesesDias,{8,'AGOSTO',31})
AADD(aMesesDias,{9,'SEPTIEMBRE',30})
AADD(aMesesDias,{10,'OCTUBRE',31})
AADD(aMesesDias,{11,'NOVIEMBRE',30})
AADD(aMesesDias,{12,'DICIEMBRE',31})

*--------------------------------------------PARA CALCULAR LOS AÑOS
IF nMesNaci < nMesHoy
   nDifAnos  := nAnoHoy - nAnoNaci
ELSEIF nMesNaci == nMesHoy
   nDifAnos  := nAnoHoy - nAnoNaci
ELSEIF nMesNaci > nMesHoy
   nDifAnos := (nAnoHoy - nAnoNaci)-1
   lAnosMenos := .T.
ENDIF

*------------------------------------PARA CALCULAR LOS MESES Y DÍAS
IF nMesNaci < nMesHoy
   nDifMeses  := (nMesHoy - nMesNaci)
   nDifDias  := Abs(nDiaHoy - nDiaNaci)
ELSEIF nMesNaci == nMesHoy
   IF nDiaNaci < nDiaHoy
      nDifMeses  := nMesHoy - nMesNaci
      nDifDias  := nDiaHoy - nDiaNaci
   ELSEIF nDiaNaci == nDiaHoy
      nDifMeses  := nMesHoy - nMesNaci
      nDifDias := 0
   ELSEIF nDiaNaci > nDiaHoy
      nDifMeses  := nMesHoy-1
      lMesesMenos:= .T.
      nDifDias := IIF(nMesNaci=1,aMesesDias[12][3]-nDiaNaci + nDiaHoy,aMesesDias[nMesNaci-1][3]-nDiaNaci + nDiaHoy)
   ENDIF   
ELSEIF nMesNaci > nMesHoy
   nDifMeses := (12-nMesNaci)+nMesHoy 
   nDifDias  := Abs(nDiaHoy - nDiaNaci)
ENDIF

aEdad:= {nDifAnos,nDifMeses,nDifDias}

RETURN aEdad
Saludos
Polo
*-----------------------------------------------
regards
Polo

Re: Año(s), mes(es) y dia(s) transcurridos entre dos fechas

Posted: Fri Mar 15, 2013 4:34 pm
by Leopoldo Blancas
Hola Walter...

Gracias por compartir. :D

Saludos
Polo
*---------------------------------------------------------------------
Hello Walter ...

Thanks for sharing. : D

regards
Polo