- Oct
- 364
- 17
This is a set of formatting routines mainly for dates. These are mainly for converting non-standard formats between systems or for displaying data in a more human-readable format (e.g., CCYY-MM-DD-hh-mm-ss rather than CCYYMMDDhhmmss)
They convert between ISO (CCYYMMDD), slash6 (MM/DD/YY), ISO-dash (CCYY-MM-DD), slash8 (MM/DD/CCYY) and various other formats.
Note that sometimes conversions will require calling two routines in a row, e.g., there is an ISO_TO_SLASH routine (slash8) and a SLASH8_TO_6 routine but no ISO_TO_SLASH6 routine; there are SLASH_TO_ISO (slash8) and ISO_WITH_DASH but no SLASH_TO_ISO_WITH_DASH.
Formatting routines are also included for:
Note: Although most of these list two parameters in the label, none of them require using a second parameter. That is only the name of the variable that will contain the value returned. None of these change the value of the first parameter.
::******************************************************
:FORMAT_SSN [SSN_in SSN_out]
::
:: Adds hyphens to SSN
::
:: Parameters: SSN_in: string in format 143210313
:: Output is returned in %SSN_out, NOT the calling varname
::
:: CALL WITH:
:: GOSUB FORMAT_SSN %SSN
:: Set SSN=%SSN_out
::
::******************************************************
IFF %@len[%@trim[%SSN_in]] EQ 9 THEN
Set SSN_out=%@left[3,%SSN_in]-%@instr[3,2,%SSN_in]-%@right[4,%SSN_in]
ELSE
Set SSN_out=
ENDIFF
RETURN
:: End of FORMAT_SSN [SSN_in SSN_out]
::******************************************************
:FORMAT_PHONE [phone_in phone_out]
::
:: Adds hyphens to 10-digit phone
::
:: Parameters: phone_in: string in format 2143210313
:: Output is returned in %phone_out, NOT the calling varname
::
:: CALL WITH:
:: GOSUB FORMAT_PHONE %calling_phone_var
:: Set calling_phone_var=%phone_out
::
::******************************************************
IFF %@len[%@trim[%phone_in]] EQ 10 THEN
Set phone_out=%@left[3,%phone_in]-%@instr[3,3,%phone_in]-%@right[4,%phone_in]
ELSE
Set phone_out=
ENDIFF
RETURN
:: End of FORMAT_PHONE [phone_in phone_out]
::******************************************************
:FORMAT_ZIP_9 [ZIP_in ZIP_out]
::
:: Adds hyphen to 9-digit ZIP Codes
::
:: Parameters: ZIP_in: string in format 752310313
:: Output is returned in %ZIP_out, NOT the calling varname
::
:: CALL WITH:
:: GOSUB FORMAT_ZIP_9 %calling_zip_var
:: Set calling_zip_var=%ZIP_out
::
::******************************************************
set ziplen=%@len[%@trim[%ZIP_in]]
IFF %ziplen EQ 9 THEN
Set ZIP_out=%@left[5,%ZIP_in]-%@right[4,%ZIP_in]
ELSEIFF %ziplen EQ 5 THEN
Set ZIP_out=%ZIP_in
ELSE
Set ZIP_out=
ENDIFF
UNSET ziplen
RETURN
:: End of FORMAT_ZIP_9 [ZIP_in ZIP_out]
::******************************************************
:ISO_DASH_TO_ISO [isodate_dash isodate]
::
:: Converts CCYY-MM-DD to CCYYMMDD e.g., 2014-03-13 > 20140313
::
:: Parameters: isodate_dash string in format 2014-03-13
:: Output is returned in %isodate, NOT the calling varname
::
:: Call with:
:: GOSUB ISO_DASH_TO_ISO %datevar
:: Set datevar=%isodate
::
::******************************************************
Set isodate=%@replace[-,,%isodate_dash]
RETURN
:: End of ISO_DASH_TO_ISO [isodate_dash isodate]
::******************************************************
:ISO_WITH_DASH [isodate isodate_dash]
::
:: Converts CCYYMMDD to CCYY-MM-DD e.g., 20140313 > 2014-03-13
::
:: Parameters: isodate: string in format 20140313
:: Output is returned in %isodate_dash, NOT the calling varname
::
:: Call with:
:: GOSUB ISO_WITH_DASH %datevar
:: Set datevar=%isodate_dash
::
::******************************************************
Set isodate_dash=%@left[4,%isodate]-%@instr[4,2,%isodate]-%@right[2,%isodate]
RETURN
:: End of ISO_WITH_DASH [isodate isodate_dash]
::******************************************************
:ISO_TO_SLASH [isodate slashdate]
::
:: Converts CCYYMMDD to MM/DD/CCYY, e.g., 20140313 > 03/13/2014
::
:: Parameters: isodate: string in format 20140313
:: Output is returned in %slashdate, NOT the calling varname
::
:: Call with:
:: GOSUB ISO_TO_SLASH %datevar
:: Set datevar=%slashdate
::
::******************************************************
Set slashdate=%@instr[4,2,%isodate]/%@right[2,%isodate]/%@left[4,%isodate]
RETURN
:: End of ISO_TO_SLASH [isodate slashdate]
::******************************************************
:SLASH_TO_ISO [slashdate isodate]
::
:: Converts MM/DD/CCYY to CCYYMMDD, e.g., 03/13/2014 > 20140313
::
:: Parameter: slashdate: string in format 03/13/2014
:: Output is returned in %isodate, NOT the calling varname
::
:: Call with:
:: GOSUB SLASH_TO_ISO %datevar
:: Set datevar=%isodate
::
::******************************************************
Set isodate=%@right[4,%slashdate]%@left[2,%slashdate]%@instr[3,2,%slashdate]
RETURN
:: End of SLASH_TO_ISO [slashdate isodate]
::******************************************************
:SLASH6_TO_8 [slashdate6 slashdate8]
::
:: Converts MM/DD/YY to MM/DD/CCYY, e.g., 03/13/14 to 03/13/2014
:: Assumes the century is 20 NOT 19
::
:: Parameter: slashdate6: string in format 03/13/14
:: Output is returned in %slashdate8, NOT the calling varname
::
:: Call with:
:: GOSUB SLASH6_TO_8 %datevar
:: Set datevar=%slashdate8
::
::******************************************************
Set slashdate8=%@left[-2,%slashdate6]20%@right[2,%slashdate6]
RETURN
:: End of SLASH6_TO_8 [slashdate6 slashdate8]
::******************************************************
:SLASH8_TO_6 [slashdate8 slashdate6]
::
:: Converts MM/DD/CCYY to MM/DD/YY, e.g., 03/13/2014 to 03/13/14
:: Assumes the century is 20 NOT 19
::
:: Parameter: slashdate6: string in format 03/13/2014
:: Output is returned in %slashdate6, NOT the calling varname
::
:: Call with:
:: GOSUB SLASH8_TO_6 %datevar
:: Set datevar=%slashdate6
::
::******************************************************
Set slashdate6=%@left[6,%slashdate8]%@right[2,%slashdate8]
RETURN
:: End of SLASH8_TO_6 [slashdate6 slashdate8]
::******************************************************
:DATE_AS_YYMMDD [YYYYMMDDx dateYYMMDD]
::
:: Parameter: YYYYMMDDx string in format YYYYMMDD<anything>
:: Output is returned in %dateYYMMDD, NOT the calling varname
:: Creates var dateYYMMDD. Century is presumed 20
::
:: Call with:
:: GOSUB DATE_AS_YYMMDD %datevar
:: Set something=%dateYYMMDD
::
::******************************************************
Set dateYYMMDD=%@left[6,%@right[-2,%YYYYMMDDx]]
RETURN
:: End of DATE_AS_YYMMDD [YYYYMMDDx dateYYMMDD]
::******************************************************
:NOW_WITH_MIN_HYPHENS
::
:: Sets var %now_w_min_hyphens
:: Returns _datetime in format YY-MM-DD-hh-mm
::
::******************************************************
Set t=%_datetime
Set t=%@instr[2,2,%t]-%@instr[4,2,%t]-%@instr[6,2,%t]-%@instr[8,2,%t]-%@instr[10,2,%t]
Set now_w_min_hyphens=%t
UNSET t
RETURN
:: End :NOW_WITH_MIN_HYPHENS
::******************************************************
:NOW_WITH_SEC_HYPHENS
::
:: Sets var %now_w_sec_hyphens
:: Returns _datetime in format YY-MM-DD-hh-mm-ss
::
::******************************************************
Set t=%_datetime
Set t=%@instr[2,2,%t]-%@instr[4,2,%t]-%@instr[6,2,%t]-%@instr[8,2,%t]-%@instr[10,2,%t]-%@right[2,%t]
Set now_w_sec_hyphens=%t
UNSET t
RETURN
:: End :NOW_WITH_SEC_HYPHENS
::******************************************************
:SLASHVARDATE_TO_YYMMDD [slashvardate dateYYMMDD]
::
:: Parameter: date string in format 7/3/20xx or 07/03/20xx or 7/03/20xx or 07/3/20xx
:: Output is returned in %dateYYMMDD, NOT the calling varname
:: Creates var dateYYMMDD. Century is presumed 20
::
:: Call with:
:: GOSUB SLASHVARDATE_TO_YYMMDD %datevar
:: Set something=%dateYYMMDD
::
::******************************************************
set monthMM=%@formatn[02.0,%@word["/",0,%slashvardate]]
set dayDD=%@formatn[02.0,%@word["/",1,%slashvardate]]
set yearYY=%@right[2,%slashvardate]
Set dateYYMMDD=%yearYY%%monthMM%%dayDD
UNSET monthMM dayDD yearYY
RETURN
:: End of SLASHVARDATE_TO_YYMMDD [slashvardate dateYYMMDD]
They convert between ISO (CCYYMMDD), slash6 (MM/DD/YY), ISO-dash (CCYY-MM-DD), slash8 (MM/DD/CCYY) and various other formats.
Note that sometimes conversions will require calling two routines in a row, e.g., there is an ISO_TO_SLASH routine (slash8) and a SLASH8_TO_6 routine but no ISO_TO_SLASH6 routine; there are SLASH_TO_ISO (slash8) and ISO_WITH_DASH but no SLASH_TO_ISO_WITH_DASH.
Formatting routines are also included for:
- Social Security Numbers
- U.S. telephone numbers with area codes
- U.S. 9-digit ZIP Codes
Note: Although most of these list two parameters in the label, none of them require using a second parameter. That is only the name of the variable that will contain the value returned. None of these change the value of the first parameter.
::******************************************************
:FORMAT_SSN [SSN_in SSN_out]
::
:: Adds hyphens to SSN
::
:: Parameters: SSN_in: string in format 143210313
:: Output is returned in %SSN_out, NOT the calling varname
::
:: CALL WITH:
:: GOSUB FORMAT_SSN %SSN
:: Set SSN=%SSN_out
::
::******************************************************
IFF %@len[%@trim[%SSN_in]] EQ 9 THEN
Set SSN_out=%@left[3,%SSN_in]-%@instr[3,2,%SSN_in]-%@right[4,%SSN_in]
ELSE
Set SSN_out=
ENDIFF
RETURN
:: End of FORMAT_SSN [SSN_in SSN_out]
::******************************************************
:FORMAT_PHONE [phone_in phone_out]
::
:: Adds hyphens to 10-digit phone
::
:: Parameters: phone_in: string in format 2143210313
:: Output is returned in %phone_out, NOT the calling varname
::
:: CALL WITH:
:: GOSUB FORMAT_PHONE %calling_phone_var
:: Set calling_phone_var=%phone_out
::
::******************************************************
IFF %@len[%@trim[%phone_in]] EQ 10 THEN
Set phone_out=%@left[3,%phone_in]-%@instr[3,3,%phone_in]-%@right[4,%phone_in]
ELSE
Set phone_out=
ENDIFF
RETURN
:: End of FORMAT_PHONE [phone_in phone_out]
::******************************************************
:FORMAT_ZIP_9 [ZIP_in ZIP_out]
::
:: Adds hyphen to 9-digit ZIP Codes
::
:: Parameters: ZIP_in: string in format 752310313
:: Output is returned in %ZIP_out, NOT the calling varname
::
:: CALL WITH:
:: GOSUB FORMAT_ZIP_9 %calling_zip_var
:: Set calling_zip_var=%ZIP_out
::
::******************************************************
set ziplen=%@len[%@trim[%ZIP_in]]
IFF %ziplen EQ 9 THEN
Set ZIP_out=%@left[5,%ZIP_in]-%@right[4,%ZIP_in]
ELSEIFF %ziplen EQ 5 THEN
Set ZIP_out=%ZIP_in
ELSE
Set ZIP_out=
ENDIFF
UNSET ziplen
RETURN
:: End of FORMAT_ZIP_9 [ZIP_in ZIP_out]
::******************************************************
:ISO_DASH_TO_ISO [isodate_dash isodate]
::
:: Converts CCYY-MM-DD to CCYYMMDD e.g., 2014-03-13 > 20140313
::
:: Parameters: isodate_dash string in format 2014-03-13
:: Output is returned in %isodate, NOT the calling varname
::
:: Call with:
:: GOSUB ISO_DASH_TO_ISO %datevar
:: Set datevar=%isodate
::
::******************************************************
Set isodate=%@replace[-,,%isodate_dash]
RETURN
:: End of ISO_DASH_TO_ISO [isodate_dash isodate]
::******************************************************
:ISO_WITH_DASH [isodate isodate_dash]
::
:: Converts CCYYMMDD to CCYY-MM-DD e.g., 20140313 > 2014-03-13
::
:: Parameters: isodate: string in format 20140313
:: Output is returned in %isodate_dash, NOT the calling varname
::
:: Call with:
:: GOSUB ISO_WITH_DASH %datevar
:: Set datevar=%isodate_dash
::
::******************************************************
Set isodate_dash=%@left[4,%isodate]-%@instr[4,2,%isodate]-%@right[2,%isodate]
RETURN
:: End of ISO_WITH_DASH [isodate isodate_dash]
::******************************************************
:ISO_TO_SLASH [isodate slashdate]
::
:: Converts CCYYMMDD to MM/DD/CCYY, e.g., 20140313 > 03/13/2014
::
:: Parameters: isodate: string in format 20140313
:: Output is returned in %slashdate, NOT the calling varname
::
:: Call with:
:: GOSUB ISO_TO_SLASH %datevar
:: Set datevar=%slashdate
::
::******************************************************
Set slashdate=%@instr[4,2,%isodate]/%@right[2,%isodate]/%@left[4,%isodate]
RETURN
:: End of ISO_TO_SLASH [isodate slashdate]
::******************************************************
:SLASH_TO_ISO [slashdate isodate]
::
:: Converts MM/DD/CCYY to CCYYMMDD, e.g., 03/13/2014 > 20140313
::
:: Parameter: slashdate: string in format 03/13/2014
:: Output is returned in %isodate, NOT the calling varname
::
:: Call with:
:: GOSUB SLASH_TO_ISO %datevar
:: Set datevar=%isodate
::
::******************************************************
Set isodate=%@right[4,%slashdate]%@left[2,%slashdate]%@instr[3,2,%slashdate]
RETURN
:: End of SLASH_TO_ISO [slashdate isodate]
::******************************************************
:SLASH6_TO_8 [slashdate6 slashdate8]
::
:: Converts MM/DD/YY to MM/DD/CCYY, e.g., 03/13/14 to 03/13/2014
:: Assumes the century is 20 NOT 19
::
:: Parameter: slashdate6: string in format 03/13/14
:: Output is returned in %slashdate8, NOT the calling varname
::
:: Call with:
:: GOSUB SLASH6_TO_8 %datevar
:: Set datevar=%slashdate8
::
::******************************************************
Set slashdate8=%@left[-2,%slashdate6]20%@right[2,%slashdate6]
RETURN
:: End of SLASH6_TO_8 [slashdate6 slashdate8]
::******************************************************
:SLASH8_TO_6 [slashdate8 slashdate6]
::
:: Converts MM/DD/CCYY to MM/DD/YY, e.g., 03/13/2014 to 03/13/14
:: Assumes the century is 20 NOT 19
::
:: Parameter: slashdate6: string in format 03/13/2014
:: Output is returned in %slashdate6, NOT the calling varname
::
:: Call with:
:: GOSUB SLASH8_TO_6 %datevar
:: Set datevar=%slashdate6
::
::******************************************************
Set slashdate6=%@left[6,%slashdate8]%@right[2,%slashdate8]
RETURN
:: End of SLASH8_TO_6 [slashdate6 slashdate8]
::******************************************************
:DATE_AS_YYMMDD [YYYYMMDDx dateYYMMDD]
::
:: Parameter: YYYYMMDDx string in format YYYYMMDD<anything>
:: Output is returned in %dateYYMMDD, NOT the calling varname
:: Creates var dateYYMMDD. Century is presumed 20
::
:: Call with:
:: GOSUB DATE_AS_YYMMDD %datevar
:: Set something=%dateYYMMDD
::
::******************************************************
Set dateYYMMDD=%@left[6,%@right[-2,%YYYYMMDDx]]
RETURN
:: End of DATE_AS_YYMMDD [YYYYMMDDx dateYYMMDD]
::******************************************************
:NOW_WITH_MIN_HYPHENS
::
:: Sets var %now_w_min_hyphens
:: Returns _datetime in format YY-MM-DD-hh-mm
::
::******************************************************
Set t=%_datetime
Set t=%@instr[2,2,%t]-%@instr[4,2,%t]-%@instr[6,2,%t]-%@instr[8,2,%t]-%@instr[10,2,%t]
Set now_w_min_hyphens=%t
UNSET t
RETURN
:: End :NOW_WITH_MIN_HYPHENS
::******************************************************
:NOW_WITH_SEC_HYPHENS
::
:: Sets var %now_w_sec_hyphens
:: Returns _datetime in format YY-MM-DD-hh-mm-ss
::
::******************************************************
Set t=%_datetime
Set t=%@instr[2,2,%t]-%@instr[4,2,%t]-%@instr[6,2,%t]-%@instr[8,2,%t]-%@instr[10,2,%t]-%@right[2,%t]
Set now_w_sec_hyphens=%t
UNSET t
RETURN
:: End :NOW_WITH_SEC_HYPHENS
::******************************************************
:SLASHVARDATE_TO_YYMMDD [slashvardate dateYYMMDD]
::
:: Parameter: date string in format 7/3/20xx or 07/03/20xx or 7/03/20xx or 07/3/20xx
:: Output is returned in %dateYYMMDD, NOT the calling varname
:: Creates var dateYYMMDD. Century is presumed 20
::
:: Call with:
:: GOSUB SLASHVARDATE_TO_YYMMDD %datevar
:: Set something=%dateYYMMDD
::
::******************************************************
set monthMM=%@formatn[02.0,%@word["/",0,%slashvardate]]
set dayDD=%@formatn[02.0,%@word["/",1,%slashvardate]]
set yearYY=%@right[2,%slashvardate]
Set dateYYMMDD=%yearYY%%monthMM%%dayDD
UNSET monthMM dayDD yearYY
RETURN
:: End of SLASHVARDATE_TO_YYMMDD [slashvardate dateYYMMDD]