Restituisce il numero di intervalli tra due date.
Sintassi
DateDiff(interval, date1, date2)
Argomenti:
Tabella 11-10 Valori dell'argomento interval
| Impostazione | Descrizione |
|---|---|
| yyyy | Anno |
| q | Trimestre |
| m | Month |
| d | Day |
| w | Giorno feriale |
| h | Hour |
| n | Minute |
| s | Second |
Formati di data supportati:
MM-DD-YYYYMM/DD/YYYYDD-MM-YYYYDD/MM/YYYYYYYY-MM-DDYYYY/MM/DDFormato di ora supportato
hh:mm:ss
Note
È possibile utilizzare la funzione DateDiff per determinare il numero di intervalli di tempo tra due date. Ad esempio, è possibile utilizzare DateDiff per calcolare il numero di giorni tra due date.
Per calcolare il numero di giorni tra date1 e date2, è possibile utilizzare Day ("d"). Se date1 fa riferimento a un point-in-time successivo a date2, la funzione DateDiff restituisce un numero negativo.
Quando si esegue il confronto tra il 31 dicembre di un anno e il 1° gennaio dell'anno successivo, DateDiff per la funzione Year ("yyyy") restituisce 1 anche se è trascorso solo un giorno.
Nell'esempio seguente viene utilizzata la funzione DateDiff per visualizzare il numero di giorni tra una data specificata e la data odierna.
Esempio 1
Function DiffADate(theDate)
DiffADate = "Days from today: " & DateDiff("d", Now, theDate)
End Function
Esempio 2
Dim StartDate, EndDate, Difference
StartDate = "01/01/2025"
EndDate = "21/01/2025"
Difference = DateDiff("d", StartDate, EndDate)
'Output: 20
Esempio 3
Dim StartDate, EndDate, Difference
StartDate = "01/01/2020"
EndDate = "01/01/2025"
Difference = DateDiff("yyyy", StartDate, EndDate)
'Output: 5
Esempio 4
Dim StartDate, EndDate, Difference
StartDate = "21/01/2025"
EndDate = "21/03/2025"
Difference = DateDiff("m", StartDate, EndDate)
'Output: 2
Esempio 5
Dim StartDate, EndDate, Difference
StartDate = "21/01/2025 08:00:00" ' Includes time component.
EndDate = "21/01/2025 18:00:00" ' Includes time component.
Difference = DateDiff("h", StartDate, EndDate)
'Output: 10
Esempio 6
Dim StartDate, EndDate, Difference
StartDate = "21/01/2025 08:00:00" ' Includes time component.
EndDate = "21/01/2025 08:45:00" ' Includes time component.
Difference = DateDiff("n", StartDate, EndDate)
'Output: 45
Esempio 7
Dim StartDate, EndDate, Difference
StartDate = "21/01/2025 08:45:00" ' Includes time component.
EndDate = "21/01/2025 08:45:50" ' Includes time component.
Difference = DateDiff("s", StartDate, EndDate)
'Output: 50