Проверьте мой алгоритм кодирования функции 'xdateago'
Function xDateAgo(ByVal D As Date, Optional ByVal StatusWord As String = "Created") As String Dim Expr$ = StatusWord + " at " Dim Hours, Minutes, Seconds, Days, Weeks, Months, Years As Integer Dim OldMonthDays, CurrentMonthDays, PlusDays As Integer Dim Value() As String = {"", "", "", ""} Dim x% = 0 6: Years = 0 Months = 0 OldMonthDays = DateTime.DaysInMonth(D.Year, D.Month) - DatePart(DateInterval.Day, D) CurrentMonthDays = DatePart(DateInterval.Day, Now) If Now.Year = D.Year Then Select Case D.Month Case Is = Now.Month PlusDays = CurrentMonthDays - DatePart(DateInterval.Day, D) Case Is < Now.Month Months = DateDiff(DateInterval.Month, DateSerial(D.Year, D.Month, DateTime.DaysInMonth(D.Year, D.Month)), DateSerial(Now.Year, Now.Month, 1)) If CurrentMonthDays >= DatePart(DateInterval.Day, D) Then PlusDays = (CurrentMonthDays) - DatePart(DateInterval.Day, D) ElseIf OldMonthDays + CurrentMonthDays >= DateTime.DaysInMonth(D.Year, D.Month) Then PlusDays = (OldMonthDays + CurrentMonthDays) - DatePart(DateInterval.Day, D) Else PlusDays = OldMonthDays + CurrentMonthDays End If End Select ElseIf Now.Year > D.Year Then Years = DateDiff(DateInterval.Year, D, Now) Months = DateDiff(DateInterval.Month, DateSerial(D.Year, D.Month, DateTime.DaysInMonth(D.Year, D.Month)), DateSerial(Now.Year, Now.Month, 1)) If Months >= (Years * 12) Then Months = Months - (Years * 12) End If If Months / 12 <= Years Then Years = Years - Months / 12 End If PlusDays = CurrentMonthDays If CurrentMonthDays >= DatePart(DateInterval.Day, D) Then PlusDays = (CurrentMonthDays) - DatePart(DateInterval.Day, D) ElseIf OldMonthDays + CurrentMonthDays >= DateTime.DaysInMonth(D.Year, D.Month) Then PlusDays = (OldMonthDays + CurrentMonthDays) - DatePart(DateInterval.Day, D) Else PlusDays = OldMonthDays + CurrentMonthDays Months -= 1 End If End If Weeks = (PlusDays \ 7) Days = If(PlusDays >= 7, PlusDays Mod 7, PlusDays) If Years > 0 Then Value(0) = String.Format("{0} Year{1}", Years, If(Years > 1, "s", "")) End If 5: If CBool(Months > 0) Then Value(1) = String.Format("{0} Month{1}", Months, If(Months > 1, "s", "")) End If If CBool(Weeks > 0) Then Value(2) = String.Format("{0} Week{1}", Weeks, If(Weeks > 1, "s", "")) End If ' { to take x Time ago... Dim T1, T2, T3 As TimeSpan T1 = D.TimeOfDay T2 = Now.TimeOfDay Hours = ((24 - T1.Hours) + T2.Hours) - 1 Minutes = ((60 - T1.Minutes) + T2.Minutes) - 1 Seconds = ((60 - T1.Seconds) + T2.Seconds) T3 = New TimeSpan(Hours, Minutes, Seconds) If T2 < T1.Add(New TimeSpan(0, 0, -1)) Then Days -= 1 End If Hours = T3.Hours Minutes = T3.Minutes Seconds = T3.Seconds ' ...} If CBool(Days > 0) Then Value(3) = String.Format("{0} Day{1}", Days, If(Days > 1, "s", "")) End If For Each v As String In Value If v.Length > 0 Then Expr += String.Format(If(x = 0, "{0}", "{1}{0}"), v, ", ") x += 1 End If Next Expr += String.Format("{1}{0}", Format(TimeSerial(Hours, Minutes, Seconds), "HH:mm:ss"), _ If(Years > 0 Or Months > 0 Or Days > 0 Or Weeks > 0, " and ", "")) Expr += " ago" Return Expr End Function
Что я уже пробовал:
Пожалуйста, проверьте мой алгоритм кодирования, чтобы сделать его лучше
Richard MacCutchan
Как же так? Мы понятия не имеем, что он должен делать.