检查我的'xdateago'函数的编码算法 [英] Check my coding algorithm of 'xdateago' function

查看:56
本文介绍了检查我的'xdateago'函数的编码算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 功能 xDateAgo( ByVal  D 作为 日期可选  ByVal  StatusWord 作为 字符串 =  创建作为 字符串 
Dim Expr $ = StatusWord + at
昏暗小时,分钟,秒,天,周,月,年 As 整数
Dim OldMonthDays,CurrentMonthDays,PlusDays 正如 整数
Dim 值()作为 String = { }
Dim x%= 0
6:年= 0
Months = 0
OldMonthDays = DateTime.DaysInMonth(D.Year,D.Month) - DatePart(DateInterval.Day,D)
CurrentMonthDays = DatePart( DateInterval.Day,Now)
如果 Now.Year = D.Year 那么
选择 案例 D.Month
案例 = Now.Month
PlusDays = CurrentMonthDays - DatePart(DateInterval.Day,D)
案例 < Now.Month
Months = DateDiff(DateInterval.Month,DateSerial(D.Year,D.Month,DateTime.DaysInMonth(D.Year,D.Month)),DateSerial(Now.Year,Now.Month,< span class =code-digit> 1
))
如果 CurrentMonthDays> = DatePart(DateInterval.Day,D)然后
PlusDays =(CurrentMonthDays) - DatePart(DateInterval.Day,D)
ElseIf OldMonthDays + CurrentMonthDays> = DateTime.DaysInMonth(D.Year,D.Month)然后
PlusDays =(OldMonthDays + CurrentMonthDays) - DatePart(DateInterval.Day, D)
其他
PlusDays = OldMonthDays + CurrentMonthDays
结束 < span class =code-keyword>如果
结束 选择
ElseIf Now.Year > D.Year 然后
年= 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 ))
如果月份> =(年* 12 那么
月份=月份 - (年* 12
结束 如果
如果月/ 12 < =年然后
年=年 - 月/ 12
< span class =code-keyword>结束 如果
PlusDays = CurrentMonthDays
如果 CurrentMonthDays> = DatePart(DateInterval.Day,D)那么
PlusDays =(CurrentMonthDays ) - DatePart(DateInterval.Day,D)
ElseIf OldMonthDays + CurrentMonthDays> = DateTime.DaysInMonth(D.Year,D.Month)然后
PlusDays =(OldMonthDays + CurrentMonthDays) - DatePart(DateInterval.Day,D)
Else
PlusDays = OldMonthDays + CurrentMonthDays
Months - = 1
结束 如果
结束 如果
周=(PlusDays \ 7
天= 如果(PlusDays> ; = 7 ,PlusDays Mod 7 ,PlusDays )

如果年> 0 然后
值( 0 )= 字符串 .Format( {0}年份{1},年,如果(年>&span; 1 s ))
结束 如果
5: 如果 CBool​​ (月份> 0 然后
值( 1 )= String .Format( {0}月{1},月份,如果(月> 1 s ))
结束 < span class =code-keyword>如果

如果 CBool​​ (周) > 0 然后
值( 2 )= 字符串 .Format( { 0}周{1},周,如果(周> 1 ,< span class =code-string> s ))
结束 如果
' {需要x时间以前...
Dim T1,T2,T3 As TimeSpan
T1 = D.TimeOfDay
T2 = Now.TimeOfDay
Hours =(( 24 - T1.Hours)+ T2.Hours) - 1
分钟=(( 60 - T1.Minutes)+ T2.Minutes) - 1
秒=(( 60 - T1.Seconds)+ T2.Seconds)
T3 = TimeSpan(小时,分钟,秒)
如果 T2< T1.Add( TimeSpan( 0 0 , - 1))然后
天 - = 1
结束 如果
Hours = T3.Hours
Minutes = T3.Minutes
秒= T3.Seconds
' ...}
如果 CBool​​ (天>&span; 0 然后
值( 3 )= 字符串。格式( {0}日{1},天,如果(天>&span; 1 s ))
结束 如果
对于 每个 v 作为 字符串
如果 v.Length> 0 然后
Expr + = String .Format( If (x = 0 {0} {1 } {0}),v,
x + = 1
结束 如果
下一步
Expr + = 字符串 .Format( {1} {0},格式(TimeSerial(小时,分钟,秒), HH:mm:ss),_
如果(是啊rs> 0 月份> 0 天> 0 周> 0 ))

Expr + = ago
返回 Expr
结束 功能





< b>我尝试了什么:



请检查我的编码算法以使更好

解决方案

< blockquote> = StatusWord + at
昏暗小时,分钟,秒,天,周,月,年作为 整数
Dim OldMonthDays,CurrentMonthDays,PlusDays As 整数
Dim 值()正如 字符串 = { < span class =code-string>, }
Dim x%= 0
6:年= 0
Months = 0
OldMonthDays = DateTime.DaysInMonth(D.Year,D.Month) - DatePart(DateInterval。 Day,D)
CurrentMonthDays = DatePart(DateInterval.Day,Now)
If Now.Year = D.Year 然后
选择 Case D.Month
Case = Now.Month
PlusDays = CurrentMonthDays - DatePart(DateInterval.Day,D)
Case < Now.Month
Months = DateDiff(DateInterval.Month,DateSerial(D.Year,D.Month,DateTime.DaysInMonth(D.Year,D.Month)),DateSerial(Now.Year,Now.Month,< span class =code-digit> 1 ))
如果 CurrentMonthDays> = DatePart(DateInterval.Day,D)然后
PlusDays =(CurrentMonthDays) - DatePart(DateInterval.Day,D)
ElseIf OldMonthDays + CurrentMonthDays> = DateTime.DaysInMonth(D.Year,D.Month)然后
PlusDays =(OldMonthDays + CurrentMonthDays) - DatePart(DateInterval.Day, D)
其他
PlusDays = OldMonthDays + CurrentMonthDays
结束 < span class =code-keyword>如果
结束 选择
ElseIf Now.Year > D.Year 然后
年= 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 ))
如果月份> =(年* 12 那么
月份=月份 - (年* 12
结束 如果
如果月/ 12 < =年然后
年=年 - 月/ 12
< span class =code-keyword>结束 如果
PlusDays = CurrentMonthDays
如果 CurrentMonthDays> = DatePart(DateInterval.Day,D)那么
PlusDays =(CurrentMonthDays ) - DatePart(DateInterval.Day,D)
ElseIf OldMonthDays + CurrentMonthDays> = DateTime.DaysInMonth(D.Year,D.Month)然后
PlusDays =(OldMonthDays + CurrentMonthDays) - DatePart(DateInterval.Day,D)
Else
PlusDays = OldMonthDays + CurrentMonthDays
Months - = 1
结束 如果
结束 如果
周=(PlusDays \ 7
天= 如果(PlusDays> ; = 7 ,PlusDays Mod 7 ,PlusDays )

如果年> 0 然后
值( 0 )= 字符串 .Format( {0}年份{1},年,如果(年>&span; 1 s ))
结束 如果
5: 如果 CBool​​ (月份> 0 然后
值( 1 )= String .Format( {0}月{1},月份,如果(月> 1 s ))
结束 < span class =code-keyword>如果

如果 CBool​​ (周) > 0 然后
值( 2 )= 字符串 .Format( { 0}周{1},周,如果(周> 1 ,< span class =code-string> s ))
结束 如果
' {需要x时间以前...
Dim T1,T2,T3 As TimeSpan
T1 = D.TimeOfDay
T2 = Now.TimeOfDay
Hours =(( 24 - T1.Hours)+ T2.Hours) - 1
分钟=(( 60 - T1.Minutes)+ T2.Minutes) - 1
秒=(( 60 - T1.Seconds)+ T2.Seconds)
T3 = TimeSpan(小时,分钟,秒)
如果 T2< T1.Add( TimeSpan( 0 0 , - 1))然后
天 - = 1
结束 如果
Hours = T3.Hours
Minutes = T3.Minutes
秒= T3.Seconds
' ...}
如果 CBool​​ (天>&span; 0 然后
值( 3 )= 字符串。格式( {0}日{1},天,如果(天>&span; 1 s ))
结束 如果
对于 每个 v 作为 字符串
如果 v.Length> 0 然后
Expr + = String .Format( If (x = 0 {0} {1 } {0}),v,
x + = 1
结束 如果
下一步
Expr + = 字符串 .Format( {1} {0},格式(TimeSerial(小时,分钟,秒), HH:mm:ss),_
如果(是啊rs> 0 月份> 0 天> 0 周> 0 ))

Expr + = ago
返回 Expr
结束 功能





< b>我尝试了什么:



请检查我的编码算法以使其更好


否。我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!


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



What I have tried:

Please Check my Coding algorithm to make Better

解决方案

= 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



What I have tried:

Please Check my Coding algorithm to make Better


No. We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


这篇关于检查我的'xdateago'函数的编码算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆