如何从给定日期获取每周/月/季度/年度的开始日期和结束日期(期间)(包括上一个和下一个)
- 2026-11-30 02:15:05
如何从给定日期获取每周/月/季度/年度的开始日期和结束日期(期间)(包括上一个和下一个)Dynamics 365 商务中心 







大家好,读者们。今天我想分享另一个关于 Business Central 的建议。在开发过程中,有时你需要从某一日期获取每周/月/季度/年度的起始日期和结束日期(包括最后和下一个)。我们怎么才能快点完成?在这篇文章中,我将简要总结,希望能给你一些帮助。
这次我们用的是CalcDate方法。
计算日期方法:基于日期表达式和参考日期计算新日期。
语法:NewDate := System.CalcDate(DateExpression: 字符串 [, Date: Date])
参数:
DateExpression类型:文本date表达式可以是任意长度。字符串从左到右解释,每次只用一个子表达式。以下规则描述了日期表达式的有效语法:
DateExpression = [<子表达式>][<子表达式>][<子表达式>] <子表达式> = [<符号>] <项>- <符号> = + |– <Term> = <Number><Unit> |<单位><编号> |<前缀><单位> <Number> = 正整数 <单位> = D |WD |W |M |问 |Y(D=日,WD=工作日,W=周,M=月,Q=季度,Y=年) <前缀>= C(C=当前)这些生成规则表明日期表达式由零、一、二或三个子表达式组成。每个子表达式由一个可选符号和一个项组成。以下是一些典型的术语示例: 30D(30天;对应<编号><单位>) WD2(工作日编号2;对应<单位><编号>) CW(本周;对应<前缀><单位>)内部日历从周一开始,周日结束。这意味着周一是工作日1,周日是工作日7。如果DateExpression的语法不正确,就会发生运行时错误。
让我们看看更多细节。为了简单测试,我用销售订单上的过账日期作为给定的日期。


本周、上周和下周 这个月、上个月和下个月 本季度、上季度和下季度 今年、去年和明年
本周、上周和下周


关键代码:
//This Week ThisWeekStartDate := CalcDate('<-CW>', GivenDate); ThisWeekEndDate := CalcDate('<CW>', GivenDate); //Last Week LastWeekStartDate := CalcDate('<-CW-1W>', GivenDate); LastWeekEndDate := CalcDate('<CW-1W>', GivenDate); //Next Week NextWeekStartDate := CalcDate('<-CW+1W>', GivenDate); NextWeekEndDate := CalcDate('<CW+1W>', GivenDate);这个月、上个月和下个月


关键代码:
//This Month ThisMonthStartDate := CalcDate('<-CM>', GivenDate); ThisMonthEndDate := CalcDate('<CM>', GivenDate); //Last Month LastMonthStartDate := CalcDate('<-CM-1M>', GivenDate); LastMonthEndDate := CalcDate('<CM>', LastMonthStartDate); //Next Month NextMonthStartDate := CalcDate('<-CM+1M>', GivenDate); NextMonthEndDate := CalcDate('<CM>', NextMonthStartDate);本季度、上季度和下季度


关键代码:
//This Quarter ThisQuarterStartDate := CalcDate('<-CQ>', GivenDate); ThisQuarterEndDate := CalcDate('<CQ>', GivenDate); //Last Quarter LastQuarterStartDate := CalcDate('<-CQ-1Q>', GivenDate); LastQuarterEndDate := CalcDate('<CQ>', LastQuarterStartDate); //Next Quarter NextQuarterStartDate := CalcDate('<-CQ+1Q>', GivenDate); NextQuarterEndDate := CalcDate('<CQ>', NextQuarterStartDate);今年、去年和明年
关键代码:
//This Year ThisYearStartDate := CalcDate('<-CY>', GivenDate); ThisYearEndDate := CalcDate('<CY>', GivenDate); //Last Year LastYearStartDate := CalcDate('<-CY-1Y>', GivenDate); LastYearEndDate := CalcDate('<CY-1Y>', GivenDate); //Next Year NextYearStartDate := CalcDate('<-CY+1Y>', GivenDate); NextYearEndDate := CalcDate('<CY+1Y>', GivenDate);测试视频:
源代码:Github(请注意,源代码仅供参考,您可以根据需要进行改进)
pageextension 50204 SalesOrderExt extends "Sales Order"{ layout { modify("Posting Date") { trigger OnAfterValidate() begin GetWeekPeriodFromGivenDate(Rec."Posting Date"); GetMonthPeriodFromGivenDate(Rec."Posting Date"); GetQuarterPeriodFromGivenDate(Rec."Posting Date"); GetYearPeriodFromGivenDate(Rec."Posting Date"); end; } } var WeekMsg: Label 'This week: %1 ~ %2\Last week: %3 ~ %4\Next week: %5 ~ %6'; MonthMsg: Label 'This month: %1 ~ %2\Last month: %3 ~ %4\Next month: %5 ~ %6'; QuarterMsg: Label 'This quarter: %1 ~ %2\Last quarter: %3 ~ %4\Next quarter: %5 ~ %6'; YearMsg: Label 'This year: %1 ~ %2\Last year: %3 ~ %4\Next year: %5 ~ %6'; local procedure GetWeekPeriodFromGivenDate(GivenDate: Date) var ThisWeekStartDate: Date; ThisWeekEndDate: Date; LastWeekStartDate: Date; LastWeekEndDate: Date; NextWeekStartDate: Date; NextWeekEndDate: Date; begin //This Week ThisWeekStartDate := CalcDate('<-CW>', GivenDate); ThisWeekEndDate := CalcDate('<CW>', GivenDate); //Last Week LastWeekStartDate := CalcDate('<-CW-1W>', GivenDate); LastWeekEndDate := CalcDate('<CW-1W>', GivenDate); //Next Week NextWeekStartDate := CalcDate('<-CW+1W>', GivenDate); NextWeekEndDate := CalcDate('<CW+1W>', GivenDate); Message(WeekMsg, ThisWeekStartDate, ThisWeekEndDate, LastWeekStartDate, LastWeekEndDate, NextWeekStartDate, NextWeekEndDate); end; local procedure GetMonthPeriodFromGivenDate(GivenDate: Date) var ThisMonthStartDate: Date; ThisMonthEndDate: Date; LastMonthStartDate: Date; LastMonthEndDate: Date; NextMonthStartDate: Date; NextMonthEndDate: Date; begin //This Month ThisMonthStartDate := CalcDate('<-CM>', GivenDate); ThisMonthEndDate := CalcDate('<CM>', GivenDate); //Last Month LastMonthStartDate := CalcDate('<-CM-1M>', GivenDate); LastMonthEndDate := CalcDate('<CM>', LastMonthStartDate); //Next Month NextMonthStartDate := CalcDate('<-CM+1M>', GivenDate); NextMonthEndDate := CalcDate('<CM>', NextMonthStartDate); Message(MonthMsg, ThisMonthStartDate, ThisMonthEndDate, LastMonthStartDate, LastMonthEndDate, NextMonthStartDate, NextMonthEndDate); end; local procedure GetQuarterPeriodFromGivenDate(GivenDate: Date) var ThisQuarterStartDate: Date; ThisQuarterEndDate: Date; LastQuarterStartDate: Date; LastQuarterEndDate: Date; NextQuarterStartDate: Date; NextQuarterEndDate: Date; begin //This Quarter ThisQuarterStartDate := CalcDate('<-CQ>', GivenDate); ThisQuarterEndDate := CalcDate('<CQ>', GivenDate); //Last Quarter LastQuarterStartDate := CalcDate('<-CQ-1Q>', GivenDate); LastQuarterEndDate := CalcDate('<CQ>', LastQuarterStartDate); //Next Quarter NextQuarterStartDate := CalcDate('<-CQ+1Q>', GivenDate); NextQuarterEndDate := CalcDate('<CQ>', NextQuarterStartDate); Message(QuarterMsg, ThisQuarterStartDate, ThisQuarterEndDate, LastQuarterStartDate, LastQuarterEndDate, NextQuarterStartDate, NextQuarterEndDate); end; local procedure GetYearPeriodFromGivenDate(GivenDate: Date) var ThisYearStartDate: Date; ThisYearEndDate: Date; LastYearStartDate: Date; LastYearEndDate: Date; NextYearStartDate: Date; NextYearEndDate: Date; begin //This Year ThisYearStartDate := CalcDate('<-CY>', GivenDate); ThisYearEndDate := CalcDate('<CY>', GivenDate); //Last Year LastYearStartDate := CalcDate('<-CY-1Y>', GivenDate); LastYearEndDate := CalcDate('<CY-1Y>', GivenDate); //Next Year NextYearStartDate := CalcDate('<-CY+1Y>', GivenDate); NextYearEndDate := CalcDate('<CY+1Y>', GivenDate); Message(YearMsg, ThisYearStartDate, ThisYearEndDate, LastYearStartDate, LastYearEndDate, NextYearStartDate, NextYearEndDate); end;}结束
希望这些对你有帮助。
感谢阅读。
本文来自网友投稿或网络内容,如有侵犯您的权益请联系我们删除,联系邮箱:wyl860211@qq.com 。