【问题标题】:How do I add 2 years to a date in powerbuilder and account for the leap year correctly?如何将 2 年添加到 powerbuilder 中的日期并正确计算闰年?
【发布时间】:2011-01-01 19:35:58
【问题描述】:

如何在 powerbuilder 中将日期添加 2 年并正确计算闰年?

我们有一个医疗许可证申请,用户希望日期过期两年。当前的许可日期是 2010 年 7 月 10 日,到期日期应该是 2012 年 7 月 2 日

我希望 relativedate 函数采用另一个参数,这样您就可以传递数字年数。

【问题讨论】:

    标签: powerbuilder leap-year


    【解决方案1】:

    这里有一个更可重用的解决方案 - 并且可以做的比你需要的更多 - 它是全局功能 - 或者对于你的 nvo 爱好者,你可以把它放在其中之一:

    >

    /* 它的作用

    传递您想要添加的日期和日期、月份、年份 并返回新日期

    用法

    您可以在 ai_days ai_months ai_years 中发送任何数字,包括负数 和大于 12 的月份(例如 ai_months = -18 表示过去 18 个月)

    论据

    日期 adt_change - 传入并返回更改日期

    整数ai_days

    整数 ai_months

    整数 ai_years

    返回整数

    */

    试试

    整数 li_add_years

    整数 li_new_day

    整数 li_new_month

    整数 li_new_year

    整数 li_total_months

    添加日期

    如果 ai_days 0 那么 adt_change = relativedate(adt_change,ai_days)

    如果 isnull(adt_change) 则抛出 STOP 结束如果

    保存更改的日期、月份和年份

    li_new_day = day(adt_change)

      if isnull(li_new_day) then throw STOP
    

    li_new_month = 月(adt_change)

      if isnull(li_new_month) then throw STOP
    

    li_new_year = 年份(adt_change)

      if isnull(li_new_year) then throw STOP
    

    更改月份

    如果 ai_months 0 则

      li_total_months = li_new_month + ai_months
    
      li_new_month = mod(li_total_months,12)
    
          if isnull(li_new_month) then throw STOP
    
      li_add_years = int(li_total_months/12)  
    
          if isnull(li_add_years) then throw STOP
    
      if li_total_months <=0 then li_new_month += 12      
    

    如果结束

    更改年份

    li_new_year += li_add_years + ai_years

    重置日期

    adt_change = 日期(li_new_year,li_new_month,li_new_day)

      if adt_change = 1900-01-01 or isnull(adt_change) then 
    

    抛出停止

    返回成功

    catch (runtimeerror lrteo_error)

    返回日志错误(lrteo_error)

    结束尝试

    【讨论】:

    • 如果你不使用 try catch 来捕获错误,你将不得不把它拿出来 - 但否则你应该能够创建一个新函数 - 我叫我的 changedate() 。 adt_change 应该通过引用传递,其余的可以通过值传递。那些对 try catch 的工作原理感兴趣的人——我有一个带有示例代码的 SourceForge Powerbuilder 项目:它在 SourceForge 上称为 OBA 或 OBAFrame。
    【解决方案2】:

    怎么样:

    // adjusted from suggestions
    IF Month (ldt_OldDate) = 2 AND Day (ldt_OldDate) = 29 THEN
       ldt_NewDate = Date (Year(ldt_OldDate) + 2, 3, 1) 
       // or ... 2, 28) whichever you think is two years from Feb 29
    ELSE
       ldt_NewDate = Date (Year(ldt_OldDate) + 2, Month (ldt_OldDate), Day (ldt_OldDate))
    END IF
    

    祝你好运,

    特里

    【讨论】:

    • 感谢两位的回答!我使用了特里的那个,它似乎可以按我的需要工作。我很高兴我“偶然发现”了这个网站!我肯定会与其他程序员分享。
    • 我认为这不太对。 2000 年 2 月 29 日之后的 2 年将是 1900 年 1 月 1 日,即它不会解析它。
    【解决方案3】:

    如果你使用 PFC,你可以这样做:

    n_cst_datetime     luo_dateTime   
    
    ld_calculate_date_to = luo_dateTime.of_relativeYear(ldt_calculate_date, 2)
    

    【讨论】:

    • 您可以从 Sybase CodeXchange (codexchange.sybase.com) 免费获取 PowerBuilder 基础类 (PFC) 的源代码。上面提到的函数实际上是在pfcapsrv.pbl中的pfc_n_cst_datetime中实现的,如果你只是想看源码的话,但是上面的函数实现方式按照PFC哲学是正确的(从不直接使用PFC级别的对象,而是扩展层的对象) .
    【解决方案4】:

    这是我使用的代码:

    //Get the license Issue Date
      ldt_calculate_date = this.GetItemDateTime(1, 'issue_date')
    
    //Add two years to the date
      ld_calculate_date = date(ldt_calculate_date)
        ld_NewDate = Date ((Year(ld_calculate_date) + 2), Month (ld_calculate_date), Day 
            (ld_calculate_date))
    
    //Subtract 1 from the date for correct expiration
      ld_calculate_date_to = date(RelativeDate(ld_NewDate, -1))
    
    //Set expiration date on my datawindow
      setitem(1,"expiration_date", ld_calculate_date_to)
    

    【讨论】:

    • 此代码将在 2 月 29 日失效。它将为您提供 01-JAN-1900 的到期日期。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    相关资源
    最近更新 更多