Автор: Andrey Fedorov
- Код: Выделить всё
' Функция получает разницу между датами в заданных единицах даты/времени
' При ReturnString=True возвращает строковое представление интервала
Public Function DateInterval(d1 As Date, d2 As Date, _
Optional Years As Variant, Optional Months As Variant, Optional Days As Variant, _
Optional Hours As Variant, Optional Minutes As Variant, Optional Seconds As Variant, _
Optional ReturnString As Boolean = False) As String
Const sr As String = ", "
Const y As String = "yyyy", m As String = "m", d As String = "d", _
h As String = "h", n As String = "n", s As String = "s"
Dim dm As Date, dd As Date, dh As Date, dn As Date, ds As Date, _
ss As String, i As Integer, sss As String
If IsMissing(Years) Then
dm = d1
Else
Years = DateDiff(y, d1, d2)
dm = DateAdd(y, Years, d1)
If dm > d2 Then
Years = Years - 1
dm = DateAdd(y, Years, d1)
End If
If ReturnString Then
ss = Right$(Format$(Years, "00"), 2)
If Right$(ss, 1) = "0" Or Left$(ss, 1) = "1" Or CInt(Right$(ss, 1)) >= 5 Then
sss = Years & " лет"
Else
sss = Years & " год"
If Right$(ss, 1) <> "1" Then sss = sss & "а"
End If
End If
End If
If IsMissing(Months) Then
dd = dm
Else
Months = DateDiff(m, dm, d2)
dd = DateAdd(m, Months, dm)
If dd > d2 Then
Months = Months - 1
dd = DateAdd(m, Months, dm)
End If
If ReturnString Then
ss = Right$(Format$(Months, "00"), 2)
If Len(sss) Then sss = sss & sr
sss = sss & Months & " месяц"
If Right$(ss, 1) = "0" Or Left$(ss, 1) = "1" Or CInt(Right$(ss, 1)) >= 5 Then
sss = sss & "ев"
Else
If Right$(ss, 1) <> "1" Then sss = sss & "а"
End If
End If
End If
If IsMissing(Days) Then
dh = dd
Else
Days = DateDiff(d, dd, d2)
dh = DateAdd(d, Days, dd)
If dh > d2 Then
Days = Days - 1
dh = DateAdd(d, Days, dd)
End If
If ReturnString Then
ss = Right$(Format$(Days, "00"), 2)
If Len(sss) Then sss = sss & sr
sss = sss & Days & " д"
If Right$(ss, 1) = "0" Or Left$(ss, 1) = "1" Or CInt(Right$(ss, 1)) >= 5 Then
sss = sss & "ней"
Else
sss = sss & IIf(Right$(ss, 1) = "1", "ень", "ня")
End If
End If
End If
If IsMissing(Hours) Then
dn = dh
Else
Hours = DateDiff(h, dh, d2)
dn = DateAdd(h, Hours, dh)
If dn > d2 Then
Hours = Hours - 1
dn = DateAdd(h, Hours, dh)
End If
If ReturnString Then
ss = Right$(Format$(Hours, "00"), 2)
If Len(sss) Then sss = sss & sr
sss = sss & Hours & " час"
If Right$(ss, 1) = "0" Or Left$(ss, 1) = "1" Or CInt(Right$(ss, 1)) >= 5 Then
sss = sss & "ов"
Else
If Right$(ss, 1) <> "1" Then sss = sss & "а"
End If
End If
End If
If IsMissing(Minutes) Then
ds = dn
Else
Minutes = DateDiff(n, dn, d2)
ds = DateAdd(n, Minutes, dn)
If ds > d2 Then
Minutes = Minutes - 1
ds = DateAdd(n, Minutes, dn)
End If
If ReturnString Then
ss = Right$(Format$(Minutes, "00"), 2)
If Len(sss) Then sss = sss & sr
sss = sss & Minutes & " минут"
If Not (Right$(ss, 1) = "0" Or Left$(ss, 1) = "1" Or CInt(Right$(ss, 1)) >= 5) Then
sss = sss & IIf(Right$(ss, 1) = "1", "а", "ы")
End If
End If
End If
If Not IsMissing(Seconds) Then
Seconds = DateDiff(s, ds, d2)
If ReturnString Then
ss = Right$(Format$(Seconds, "00"), 2)
If Len(sss) Then sss = sss & sr
sss = sss & Seconds & " секунд"
If Not (Right$(ss, 1) = "0" Or Left$(ss, 1) = "1" Or CInt(Right$(ss, 1)) >= 5) Then
sss = sss & IIf(Right$(ss, 1) = "1", "а", "ы")
End If
End If
End If
If ReturnString Then DateInterval = sss
End Function
Функция, возвращающая интервал между двумя датами. Возвращаются интервалы от дня до секунды.
Автор: alibek
- Код: Выделить всё
Function DeltaDate(ByVal Date1 As Date, ByVal Date2 As Date, Optional ByVal ItemSeparator As String = " ", Optional ByVal SecondsSuffix As String, Optional ByVal MinutesSuffix As String, Optional ByVal HoursSuffix As String, Optional ByVal DaysSuffix As String) As String
Const SM As Long = 60&, MH As Long = 60&, HD As Long = 24&
Dim D As Long, H As Long, M As Long, S As Long
If Date2 > Date1 Then
S = (Date2 - Date1) * SM * MH * HD
Else
S = (Date1 - Date2) * SM * MH * HD
End If
If Len(DaysSuffix) > 0 Then
M = Fix(S / SM)
S = S - M * SM
H = Fix(M / MH)
M = M - H * MH
D = Fix(H / HD)
H = H - D * HD
DeltaDate = Trim$(Str$(D)) & DaysSuffix _
& IIf(Len(HoursSuffix) = 0, vbNullString, ItemSeparator & Trim$(Str$(H)) & HoursSuffix) _
& IIf(Len(MinutesSuffix) = 0, vbNullString, ItemSeparator & Trim$(Str$(M)) & MinutesSuffix) _
& IIf(Len(SecondsSuffix) = 0, vbNullString, ItemSeparator & Trim$(Str$(S)) & SecondsSuffix)
ElseIf Len(HoursSuffix) > 0 Then
M = Fix(S / SM)
S = S - M * SM
H = Fix(M / MH)
M = M - H * MH
DeltaDate = Trim$(Str$(H)) & HoursSuffix _
& IIf(Len(MinutesSuffix) = 0, vbNullString, ItemSeparator & Trim$(Str$(M)) & MinutesSuffix) _
& IIf(Len(SecondsSuffix) = 0, vbNullString, ItemSeparator & Trim$(Str$(S)) & SecondsSuffix)
ElseIf Len(MinutesSuffix) > 0 Then
M = Fix(S / SM)
S = S - M * SM
DeltaDate = Trim$(Str$(M)) & MinutesSuffix _
& IIf(Len(SecondsSuffix) = 0, vbNullString, ItemSeparator & Trim$(Str$(S)) & SecondsSuffix)
Else
DeltaDate = Format$(S, "0") & SecondsSuffix
End If
End Function
Пример использования:
Immediate писал(а):?DeltaDate(Now(),Now()-0.74,", "," с."," м."," ч."," д.")
0 д., 17 ч., 45 м., 36 с.