任何日期都应该转换为 cobol 中的月末日期吗?
我有一个要求,任何日期 (DD.MM.YYYY) 应转换为该月的最后一个日期(例如:如果日期是 20.01.1999 那么它应转换为 31.01.1999)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有一个要求,任何日期 (DD.MM.YYYY) 应转换为该月的最后一个日期(例如:如果日期是 20.01.1999 那么它应转换为 31.01.1999)?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
您到底遇到什么麻烦了? COBOL 还是算法?我猜是 COBOL。
我不会直接给你答案,因为你
显然倾向于语言,并且制定具体细节是有价值的
为了你自己。
这里有一些提示:
在
WORKING-STORAGE
中定义一个日期字段,以便您可以将日、月和年作为单独的项目挑选出来。类似于:注意未命名的
PIC X
字段。它们包含日/月/年分隔符。不需要为它们提供数据名称,因为您不需要引用它们。有时会给出这种类型的数据项
名称
FILLER
,但名称是可选的。阅读
EVALUATE
语句。这是链接到IBM Enterprise COBOL 手册。
EVALUATE
的描述在所有版本的 COBOL 中都应该类似。移动
感兴趣的日期TO TEST-DATE
。现在,您可以将年、月和日作为单独的项目进行引用:TEST-DAY
、TEST-MONTH
和TEST-YEAR
。使用
EVALUATE
测试月份 (TEST-MONTH
)。如果该月有 30 天,则MOVE
30 到TEST-DAY
。做同样的事情每月 31 天。由于闰年,二月是一个特例。一旦确定该月份是二月,
测试
TEST-YEAR
来确定是否是闰年并根据测试结果将
MOVE
28 或29 移至TEST-DAY
。现在,
TEST-DATE
将包含您要查找的日期。将其移动
到任何需要的地方。Exactly what are you having trouble with? COBOL or the algorithm? I'm guessing its COBOL.
I'm not going to give you a direct answer because you are
obviously leaning the language and there is value in working out the specific details
for yourself.
Here are a couple of hints:
Define a date field in
WORKING-STORAGE
so that you can pick out the day, month and year as separate items. Something like:Note the unnamed
PIC X
fields. These contain the day/month/year delimiters. They do not need to be given data names becauseyou do not need to reference them. Sometimes this type of data item is given
the name
FILLER
, but the name is optional.Read up on the
EVALUATE
statement. Here is a link tothe IBM Enterprise COBOL manual. This description of
EVALUATE
should be similar in all versions of COBOL.MOVE
the date of interestTO TEST-DATE
. Now you can reference the year, month and day as individual items:TEST-DAY
,TEST-MONTH
andTEST-YEAR
.Use
EVALUATE
to test the month (TEST-MONTH
). If the month is a 30 day month thenMOVE
30 toTEST-DAY
. Do the same for31 day months. February is a special case because of leap years. Once you have determined that the month is February,
test
TEST-YEAR
to determine if it is a leap yearand
MOVE
28 or 29 TOTEST-DAY
depending on the outcome of the test.Now
TEST-DATE
will contain the date you are looking for.MOVE
it to wherever it is needed.您可以使用函数integer-of-date,它返回与任何日期相对应的整数值。假设您的输入日期采用 ddmmyyyy 格式,并且您希望输出采用相同的格式。假设日期是 20011999,而您想要的日期是 31011999。您可以按照以下步骤操作。
请注意,您必须再添加一张支票来处理 12 月。
You can use function integer-of-date which gives returns an integral value corresponding to any date. Assuming your input date is in ddmmyyyy format and you expect hte output in the same format. Lets say date is 20011999 and you want as 31011999. You can follow the below steps.
Note here you will have to add one more check for handling December month.
干得好!在此处运行代码
此解决方案与@NealB的答案密切相关。
Here you go! Run the code here
This solution goes hand in hand with @NealB's answer.
过程“计算月结束日期”不需要对闰年或十二月进行任何检查。
结果:
虽然这对于新手 COBOL 程序员来说可能有点令人畏惧,但有一个简单的解释。过程“compute-month-end-date”由两个相同的部分组成,但“+32”除外。
首先考虑第二部分,它从日期整数中减去月份中的某一天,给出该月“第 0”天的整数值。这正是上个月最后一天的整数值。以下计算给出“yyyymmdd”格式的日期。
第一部分的作用相同,只是它添加 32 以获取下个月的日期,从 1 号到 4 号,具体取决于原始月份的天数。
综合起来
19990120
先改为19990201
,再改为19990131
。然后从20040220
到20040303
,然后是20040229
。20051220
到20060101
,然后是20051231
。The procedure "compute-month-end-date" does not require any checks for leap year or December.
Results:
While this might be a bit daunting to the novice COBOL programmer, there is a simple explanation. The procedure "compute-month-end-date" consists of two identical parts with the exception of the "+32".
Taking the second part first, it subtracts the day of month from the integer of a date giving the integer value for the 'zeroth' day of the month. This is precisely the integer value for the last day of the prior month. The following compute gives the date in 'yyyymmdd' format.
The first part does the same, except that it adds 32 to get a date in the following month, the 1st through the 4th, depending on the number of days in the original month.
Taken togther
19990120
is first changed to19990201
, then changed to19990131
. And20040220
to20040303
, then20040229
.20051220
to20060101
, then20051231
.