如何轉換美式日期 "March 18, 2004" 至 RPGLE 的 Date 格式?(API CEEDAYS CEEDATE)
由於現有國際化的環境中,常會收到國外的資料,含有文字性敘述的日期形態,所以有此
轉換需求,要做日期格式間的轉換需要利用 API CEEDAYS, CEEDATE.
下述範例將 October 08, 2002 , October 8, 2002 , Oct 08, 2002 或Oct 8, 2002
轉換為 20002-10-08, 記住不管日期是 08 或 8 與月之間僅能有一個空白,
若是 October 8, 執行時就會錯誤,此範例所執行的最後一斷測試程式碼,就會錯誤,
請回 C (Cancel)。
File : QRPGLESRC
Member: CVTDATEFMT
Type : RPGLE
Usage : CRTBNDPGM CVTDATEFMT
CALL CVTDATEFMT
H DFTACTGRP(*NO)
*
* API to parse a character date into lilian format
*
D CEEDAYS PR opdesc
D char_date 80A const options(*varsize)
D date_format 80A const options(*varsize)
D lilian_date 10I 0
D feedback 12A options(*omit)
*
* API to create character date from lilian format
*
D CEEDATE PR opdesc
D lilian_date 10I 0 const
D date_format 80A const options(*varsize)
D char_date 80A options(*varsize)
D feedback 12A options(*omit)
D fmt1 c 'Mmmmmmmmmz ZD, YYYY'
D fmt2 c 'ZM/ZD/YYYY'
D iso c 'YYYYMMDD'
D mydate s 50A
D temp s 8A
D lil s 10I 0
D DateFld s D
** Test parsing date with a leading zero in the month.
** DateFld should result in 2002-10-08
**
c eval mydate = 'October 08, 2002'
c callp CEEDAYS(mydate: fmt1: lil: *omit)
c callp CEEDATE(lil: iso: temp: *omit)
c eval DateFld = %date(temp: *ISO0)
c dsply DateFld
**
** Same date without a leading zero.
**
c eval mydate = 'October 8, 2002'
c callp CEEDAYS(mydate: fmt1: lil: *omit)
c callp CEEDATE(lil: iso: temp: *omit)
c eval DateFld = %date(temp: *ISO0)
c dsply DateFld
**
** A different date with a shorter month name
**
c eval mydate = 'May 21, 1999'
c callp CEEDAYS(mydate: fmt1: lil: *omit)
c callp CEEDATE(lil: iso: temp: *omit)
c eval DateFld = %date(temp: *ISO0)
c dsply DateFld
**
** You can also go the other way, of course. This formats
** today's date nicely.
**
c eval temp = %char(%date(): *ISO0)
c callp CEEDAYS(temp: iso: lil: *omit)
c callp CEEDATE(lil: fmt1: mydate: *omit)
c dsply MyDate
**
** There's also the "slash date" format
**
c eval mydate = '01/01/2004'
c callp CEEDAYS(mydate: fmt2: lil: *omit)
c callp CEEDATE(lil: iso: temp: *omit)
c eval DateFld = %date(temp: *ISO0)
c dsply DateFld
**
** It also works without the leading zeroes
**
c eval mydate = '2/2/2004'
c callp CEEDAYS(mydate: fmt2: lil: *omit)
c callp CEEDATE(lil: iso: temp: *omit)
c eval DateFld = %date(temp: *ISO0)
c dsply DateFld
**
** This one, however, will fail...
** (it fails on the extra space before the 1)
**
c eval mydate = 'September 1, 2004'
c callp CEEDAYS(mydate: fmt1: lil: *omit)
c callp CEEDATE(lil: iso: temp: *omit)
c eval DateFld = %date(temp: *ISO0)
c dsply DateFld
c eval *inlr = *on
執行範例結果如下:
Display Program Messages
DSPLY 2002-10-08
*N
DSPLY 2002-10-08
*N
DSPLY 1999-05-21
*N
DSPLY April 5, 2004
*N
DSPLY 4/5/2004
*N
DSPLY 2004-01-01
*N
DSPLY 2004-02-02
*N
The call to CEEDAYS ended in error (C G D F).
Type reply, press Enter.
Reply . . .
F3=Exit F12=Cancel
A blog about IBM i (AS/400), MQ and other things developers or Admins need to know.
星期一, 11月 06, 2023
2004-04-05 如何轉換美式日期 "March 18, 2004" 至 RPGLE 的 Date 格式?(API CEEDAYS CEEDATE)
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言