【问题标题】:aspclassic - Datediff treat every month with 30 daysasp classic - Datediff 每月治疗 30 天
【发布时间】:2016-01-29 22:13:45
【问题描述】:

如何从恰好 30 天的范围内计算每个月的 DateDiff。

示例:

2016-03-01  2016-03-31 = 30 days
2016-04-01  2016-04-30 = 30 days
2016-05-01  2016-05-31 = 30 days
2016-06-01  2016-06-30 = 30 days
total= 120 days

使用 DateDiff

datediff("d",cdate("2016-3-01"),cdate("2016-6-30"))    
return = 121 days

我需要他用 365 考虑 30 天的月份和年份

【问题讨论】:

    标签: asp-classic datediff


    【解决方案1】:

    有趣的问题,因为它并不像看起来那么简单。从代码效率的角度来看,正确的答案取决于您使用的代码语言。但是,一般来说,考虑一下;

    Sub Days_of_30()
    ' declare some Variables
    
      Dim YBeg, YEnd, Y  'Years: Start, End, Loop Yr Val
      Dim M1, M2         'Months
      Dim iDif           'Day Dif
      Dim nxtYR          'Required coz no GoTo
    
     //parameters here
       YBeg = 2016        'Set Start Year
       YEnd = 2019        'Set End Year
    
     //General Code 
    
      For Y = YBeg To YEnd
       nxtYR = Y + 0
         For M1 = 1 To 12  'Loop each Month
    
         M2 = M1 + 1
    
         Select Case M1
           Case 12
             nxtYR = Y + 1
             M2 = 1
         End Select
    
         iDif = DateDiff("d", CDate(M1 & "/" & 1 & "/" & Y), _
                              CDate(M2 & "/" & 1 & "/" & nxtYR))
    
         //*** Output here
         If iDif = 30 Then Debug.Print CDate(M1 & "/" & 1 & "/" & Y)
       Next
     Next
    
    End Sub 
    

    这应该在参数范围内输出所有 30 天的月份。显然,使用 GoTo 语句可以使代码更高效(更快),但是,有些人不知道如何有效地使用它们,所以大多数“现代”语言不使用 GoTo(耻辱,叹息)。

    假设您使用的脚本语言没有隐式声明 var 类型,上述方法应该可以工作,但理想情况下,如果您选择的语言支持 var 类型声明,您应该使用它们。

    希望你觉得这很有用。

    加里

    【讨论】:

      猜你喜欢
      • 2020-09-18
      • 1970-01-01
      • 2013-04-24
      • 2022-06-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多