[英]ABAP query regarding adding months to a date
I have been given an assignment like this: 我得到了这样的作业:
I will have a selection screen input box where user enters date and an other input box where he enters number of months. 我将有一个用户输入日期的选择屏幕输入框和一个他输入月份的另一个输入框。
I have to do is add the given number of months to date and get the new date. 我要做的是添加给定的月份数并获得新的日期。
Example: If entered date is 2/3/2011
and given number of months are 5
i should get 7/3/2011
. 示例:如果输入的日期是2/3/2011
月5
日,并且给定的月数是5
我应该得到7/3/2011
。
I know that system date variable 'SY-DATUM' has application server date. 我知道系统日期变量'SY-DATUM'有应用服务器日期。 And to do date manipulation i can say SY-DATUM + 2
.i can only add days,how should i add months. 并且做日期操作我可以说SY-DATUM + 2
.i只能添加天数,我应该如何添加月份。
Please give me a head start.Thanks. 请给我一个良好的开端。谢谢。
An easy way to add a number of months to a date is to use the MONTH_PLUS_DETERMINE
function module. 添加几个月的简单方法是使用MONTH_PLUS_DETERMINE
功能模块。
Use something like: 使用类似的东西:
data: mydate type sy-datum.
mydate = sy-datum.
call function 'MONTH_PLUS_DETERMINE'
exporting
MONTHS = 5
OLDDATE = mydate
importing
NEWDATE = mydate.
( MONTHS
can be negative if you want to subtract.) (如果你想减去, MONTHS
可能是负数。)
There are a lot of other helper functions for date and time calculations. 日期和时间计算还有很多其他辅助函数。 Useful ABAP Function Modules on the SDN Wiki is a good place to go when you're looking for things like that. 当您正在寻找类似的东西时,SDN Wiki上有用的ABAP功能模块是一个不错的选择。
@Mat gave the correct answer. @Mat给出了正确的答案。 If you want to do it without a function module (whatever your reason may be) and following your example in your question, you can approach your problem like so: 如果你想在没有功能模块的情况下(无论你的理由是什么)并在你的问题中遵循你的例子,你可以像这样处理你的问题:
data: lv_month type i;
lv_month = sy-datum+4(2).
sy-datum+4(2) = lv_month + 5.
// Check if months are > 12, if so, subtract 12 and increase sy-datum+0(4). Remember to loop if the input can be greater than 12 months.
As @Mat mentioned, don't reinvent the wheel. 正如@Mat所说,不要重新发明轮子。 The approach above isn't pretty and function modules get you to your destination faster. 上面的方法并不漂亮,功能模块可以让您更快地到达目的地。
Simply pass months and date to below FM to get new date. 只需将月份和日期传递到FM下方即可获得新日期。
DATA : LV_NEWDATE TYPE SY-DATUM.
CALL FUNCTION 'BKK_ADD_MONTH_TO_DATE'
EXPORTING
MONTHS = LV_MONTH " pass months to add
OLDDATE = LV_DATE "pass date here
IMPORTING
NEWDATE = LV_NEWDATE. "get new date
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.