Created
February 10, 2014 08:43
-
-
Save aMarCruz/12fcbd84fd4cc28e448c to your computer and use it in GitHub Desktop.
Devuelve la diferencia en años y meses de una a otra fecha.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*------------------------------------------------------------------------------ | |
* nResult = DateDiff(tFromDate, tToDate) | |
*------------------------------------------------------------------------------ | |
* Devuelve un número con la diferencia en años y meses de tFromDate a tToDate. | |
* Los meses están representados con decimales. | |
* P.Ej. ? DateDiff({^1961/03/31}, {^2013/08/18}) | |
* Imprime 52.04 (52 años y 4 meses) | |
*============================================================================== | |
LPARAMETERS T1 AS Date, T2 AS Date | |
LOCAL y1, m1, d1, y2, m2, d2, DD, nSign | |
IF EMPTY(m.T2) | |
m.T2 = DATE() | |
ENDIF | |
m.nSign = SIGN(m.T2 - m.T1) | |
IF m.nSign = 0 | |
RETURN 0.0 | |
ENDIF | |
IF m.nSign = -1 | |
m.y1 = m.T1 | |
m.T1 = m.T2 | |
m.T2 = m.y1 | |
ENDIF | |
m.y1 = YEAR(m.T1) && menor | |
m.m1 = MONTH(m.T1) | |
m.d1 = DAY(m.T1) | |
m.y2 = YEAR(m.T2) && mayor | |
m.m2 = MONTH(m.T2) | |
m.d2 = DAY(m.T2) | |
* Si feb-29 está en T1, ajustamos el año actual | |
IF m.d1 = 29 AND m.m1 = 2 AND m.d2 = 28 AND m.m2 = 2 | |
IF NOT IsLeap(m.y2) | |
m.d1 = 28 | |
ENDIF | |
ENDIF | |
* Obtenemos los meses, DD los estandarizará. | |
m.DD = MAX(m.d1, m.d2) | |
m.m1 = m.m1 * m.DD + m.d1 | |
m.m2 = m.m2 * m.DD + m.d2 | |
* m.d2 = m.d2 - m.d1 | |
* Primero los meses | |
m.y2 = m.y2 - m.y1 | |
IF (m.m2 < m.m1) | |
m.y2 = m.y2 - 1 && m2 es mayor, aun no se completa este año | |
m.m2 = 11 - INT((m.m2 - m.m1) / m.DD) | |
ELSE | |
m.m2 = INT((m.m2 - m.m1) / m.DD) | |
ENDIF | |
RETURN (m.y2 + (m.m2 / 100)) * m.nSign | |
*++ | |
* Devuelve True si el año dado por nYear es bisiesto. | |
*-- | |
FUNCTION IsLeap(nYear AS Integer) AS Boolean | |
RETURN MOD(m.nYear,4) == 0 AND MOD(m.nYear,100) != 0 OR MOD(m.nYear,400) == 0 | |
ENDFUNC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment