Die Frage, ob es sich zwei Datums-Angaben um den gleichen Monat
handelt, scheint auf den ersten Blick leicht zu beantworten zu sein:
If Month(Date1) = Month(Date2) Then...
Das ist aber ein Fehlschluss - nämlich dann, wenn die
Datums-Angaben aus verschiedenen Jahren sind. Nun könnten Sie zwar
auch noch das Jahr prüfen, aber es geht einfacher: Mit der Visual
Basic-Funktion DateDiff
ermitteln Sie statt dessen die tatsächliche Differenz zwischen den
Monaten. Ist die Differenz gleich 0, handelt es sich um
exakt denselben Monat.
Public Function IsSameMonth _
(ByVal Date1 As Date, ByVal Date2 As Date) As Boolean
IsSameMonth = Not CBool(DateDiff("m", Date1, Date2))
End Function
Das gleiche Prinzip können Sie natürlich auch für alle anderen
Datums-Bestandteilen verwenden - für Quartal, Woche, Tag, Stunde,
Minute und sogar Sekunde:
Public Function IsSameQuarter _
(ByVal Date1 As Date, ByVal Date2 As Date) As Boolean
IsSameQuarter = Not CBool(DateDiff("q", Date1, Date2))
End Function
Public Function IsSameWeek _
(ByVal Date1 As Date, ByVal Date2 As Date) As Boolean
IsSameWeek = Not CBool(DateDiff("w", Date1, Date2))
End Function
Public Function IsSameDay _
(ByVal Date1 As Date, ByVal Date2 As Date) As Boolean
IsSameDay = Not CBool(DateDiff("d", Date1, Date2))
End Function
Public Function IsSameHour _
(ByVal Date1 As Date, ByVal Date2 As Date) As Boolean
IsSameHour = Not CBool(DateDiff("h", Date1, Date2))
End Function
Public Function IsSameMinute _
(ByVal Date1 As Date, ByVal Date2 As Date) As Boolean
IsSameMinute = Not CBool(DateDiff("n", Date1, Date2))
End Function
Public Function IsSameSecond _
(ByVal Date1 As Date, ByVal Date2 As Date) As Boolean
IsSameSecond = Not CBool(DateDiff("s", Date1, Date2))
End Function
|