Рассчитать дни/месяц до следующего дня рождения
Кажется, я начинаю терять сюжет здесь....Это вроде как наполовину работа, но я думаю, что, возможно, слишком долго смотрел на нее и теперь не вижу Форреста за всеми деревьями....Любая идея, как улучшить это, приветствуется
Private ReadOnly CurrentDay As Date = Today Private Property Age As Integer = Nothing Private Property myBirthDay As Date = Nothing Private Property DaysToNextBirthDay As Long = 0 Private Property NewBirthDate As Date = Nothing Friend Sub CalculateNextBirthdayAndAge(ByVal ThisBirthDate As Date) Try myBirthDay = ThisBirthDate 'keep a copy a original birthdate If ThisBirthDate.Month > CurrentDay.Month AndAlso ThisBirthDate.Month <= 12 Then 'Birthday is after today, but this year......Working... NewBirthDate = CDate(FormatDateTime(CDate(String.Format("{0}-{1}-{2}", CurrentDay.Year, ThisBirthDate.Month, ThisBirthDate.Day)), DateFormat.ShortDate)) DaysToNextBirthDay = (-(DateDiff("d", NewBirthDate, CurrentDay))) ElseIf ThisBirthDate.Month < CurrentDay.Month AndAlso ThisBirthDate.Month >= 1 Then 'Birthday was ealier this year, so increase with one year for next birthday NewBirthDate = CDate(FormatDateTime(CDate(String.Format("{0}-{1}-{2}", CurrentDay.Year + 1, ThisBirthDate.Month, ThisBirthDate.Day)), DateFormat.ShortDate)) DaysToNextBirthDay = (DateDiff("d", CurrentDay, NewBirthDate)) ElseIf ThisBirthDate.Month = CurrentDay.Month Then 'Birthday is same month as current month. If ThisBirthDate.Day > CurrentDay.Day Then 'Same month, but later this month NewBirthDate = CDate(FormatDateTime(CDate(String.Format("{0}-{1}-{2}", CurrentDay.Year, ThisBirthDate.Month, ThisBirthDate.Day)), DateFormat.ShortDate)) DaysToNextBirthDay = (-(DateDiff("d", NewBirthDate, CurrentDay))) ElseIf ThisBirthDate.Day < CurrentDay.Day Then 'same month but ealier this month NewBirthDate = CDate(FormatDateTime(CDate(String.Format("{0}-{1}-{2}", CurrentDay.Year + 1, ThisBirthDate.Month, ThisBirthDate.Day)), DateFormat.ShortDate)) DaysToNextBirthDay = (DateDiff("d", CurrentDay, NewBirthDate)) Else 'Birthday today MessageBox.Show("Happy Birthday", "Happy Birthday", MessageBoxButtons.OK) End If End If 'Age = CInt(CInt(DateDiff(DateInterval.Month, myBirthDay, Today) / 12) & " år.") lblAge.Text = CInt(CInt(DateDiff(DateInterval.Month, myBirthDay, Today) / 12) & " år.").ToString 'Age.ToString & " år." lblDaysToBirthDays.Text = DaysToNextBirthDay.ToString & " dage til næste fødseksdag..." myBirthDay = Nothing DaysToNextBirthDay = Nothing NewBirthDate = Nothing Age = Nothing Catch ex As Exception MessageBox.Show(ex.Message.ToString) End Try End Sub</pre>