Gambas Documentation - /lang/datediff

# DateDiff

Count = DateDiff ( Date1 AS Date , Date2 AS Date , Period AS Integer ) AS Integer

Returns the number of periods between two dates.

- Date1 is the upper date.

- Date2 is the lower date.

- Period is the kind of period: year, month, day, week...

The number of periods is returned.

Period can be one of the following constants:

Constant | Effect
--- | ---
`gb.Second` | Return the number of seconds.
`gb.Minute` | Return the number of minutes.
`gb.Hour` | Return the number of hours.
`gb.Day` | Return the number of days.
`gb.Week` | Return the number of weeks.
`gb.WeekDay` | Return the number of week days (ignore Saturday and Sunday).
`gb.Month` | Return the number of months.
`gb.Quarter` | Return the number of quarters.
`gb.Year` | Return the number of years.

Only entire periods are returned. The result is round down.

### Examples

    PRINT DateDiff("01/02/2005 12:55:00", "01/01/2005", gb.Day)

    -1

This example shows how DateDiff works internally

    DIM date1 AS Date
    DIM date2 AS Date
    DIM fDiff AS Float
    DIM iDiff AS Integer

    date1 = Date(1964, 02, 28, 0, 29, 0)
    date2 = Date(1964, 03, 01, 0, 30, 0) ' one minute more than two days
    fDiff = CFloat(date2) - CFloat(date1)
    iDiff = Int(fDiff * 24)
    PRINT "first  : "; fdiff; "  int:"; idiff; "  DateDiff:"; DateDiff(date1, date2, gb.Hour)
    date1 = Date(1964, 02, 28, 0, 30, 0)
    date2 = Date(1964, 03, 01, 0, 29, 0) ' one minute less than two days
    fDiff = CFloat(date2) - CFloat(date1)
    iDiff = Int(fDiff * 24)
    PRINT "second : "; fdiff; "  int:"; idiff; "  DateDiff:"; DateDiff(date1, date2, gb.Hour)

    first  : 2.000694444403  int:48  DateDiff:48
    second : 1.999305555597  int:47  DateDiff:47

### See also

- Date & Time Functions