在COBOL中格式化DATE-TIME [英] formatting DATE-TIME IN COBOL
问题描述
我想格式化功能日期时间。我拥有的是
i want to format the FUNCTION DATE-TIME. What i have is
MOVE FUNCTION CURRENT-DATE TO WS-CURRENT-DATE-DATA
DISPLAY WS-CURRENT-DATE-DATA
我尝试使用填充符
01 WS-CURRENT-DATE-DATA.
05 WS-CURRENT-DATE.
10 WS-CURRENT-YEAR PIC 9(04).
10 FILLER PIC X(01) VALUE "-".
10 WS-CURRENT-MONTH PIC 9(02).
10 WS-CURRENT-DAY PIC 9(02).
05 WS-CURRENT-TIME.
10 WS-CURRENT-HOURS PIC 9(02).
10 WS-CURRENT-MINUTE PIC 9(02).
10 WS-CURRENT-SECOND PIC 9(02).
10 WS-CURRENT-MILLISECONDS PIC 9(02).
我得到的是后面带有连字符的结果
what I get is a result with the hyphen at the back like
2017122818242863-
但是我
2017-12-28-18:24:28:63
请帮助。预先感谢
推荐答案
之所以在结尾处看到此连字符,是因为这是实际的数据定义预期的功能日期
:
The reason that you are seeing this hyphen at then end is because this is actual data definition FUNCTION CURRENT-DATE
is expecting:
05 WS-CURRENT-DATE-TIME.
10 WS-CURRENT-DATE.
15 WS-CURRENT-YEAR PIC 9(4).
15 WS-CURRENT-MONTH PIC 9(2).
15 WS-CURRENT-DAY PIC 9(2).
10 WS-CURRENT-TIME.
15 WS-CURRENT-HOUR PIC 9(2).
15 WS-CURRENT-MIN PIC 9(2).
15 WS-CURRENT-SEC PIC 9(2).
15 WS-CURRENT-MS PIC 9(2).
10 WS-DIFF-GMT PIC S9(4).
您会注意到我在格林威治标准时差的末尾有一个额外字段。同样,您不能只将填充符添加到定义中并期望该函数知道如何处理它。基本上,正在发生的事情是您所放入的连字符(在picture子句中)被从函数传递的数据覆盖,您需要的是工作存储中的2个地方。
You will notice that I have an extra field at the end for Greenwich Mean Time Difference. Also, you cannot just add filler to your definition and expect that function to know what to do with it. Basically what is happening is the hyphen that you are putting in (in the picture clause) is being overwritten by the data passed from the function what you need is 2 places in working storage. One to hold the return from the function and one to hold the formatted value, so something like this:
01 WS-TEMP-DT.
05 WS-TEMP-DATE-TIME.
10 WS-TEMP-DATE.
15 WS-TEMP-YEAR PIC 9(4).
15 WS-TEMP-MONTH PIC 9(2).
15 WS-TEMP-DAY PIC 9(2).
10 WS-TEMP-TIME.
15 WS-TEMP-HOUR PIC 9(2).
15 WS-TEMP-MIN PIC 9(2).
15 WS-TEMP-SEC PIC 9(2).
15 WS-TEMP-MS PIC 9(2).
10 WS-DIFF-GMT PIC S9(4).
01 WS-FORMATTED-DT.
05 WS-FORMATTED-DATE-TIME.
15 WS-FORMATTED-YEAR PIC 9(4).
15 FILLER PIC X VALUE '-'.
15 WS-FORMATTED-MONTH PIC 9(2).
15 FILLER PIC X VALUE '-'.
15 WS-FORMATTED-DAY PIC 9(2).
15 FILLER PIC X VALUE '-'.
15 WS-FORMATTED-HOUR PIC 9(2).
15 FILLER PIC X VALUE ':'.
15 WS-FORMATTED-MIN PIC 9(2).
15 FILLER PIC X VALUE ':'.
15 WS-FORMATTED-SEC PIC 9(2).
15 FILLER PIC X VALUE ':'.
15 WS-FORMATTED-MS PIC 9(2).
MOVE FUNCTION CURRENT-DATE TO WS-TEMP-DATE-TIME
MOVE WS-TEMP-YEAR TO WS-FORMATTED-YEAR
MOVE WS-TEMP-MONTH TO WS-FORMATTED-MONTH
MOVE WS-TEMP-DAY TO WS-FORMATTED-DAY
MOVE WS-TEMP-HOUR TO WS-FORMATTED-HOUR
MOVE WS-TEMP-MIN TO WS-FORMATTED-MIN
MOVE WS-TEMP-SEC TO WS-FORMATTED-SEC
MOVE WS-TEMP-MS TO WS-FORMATTED-MS
DISPLAY WS-FORMATTED-DATE-TIME
这篇关于在COBOL中格式化DATE-TIME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!