Hello all,
Does anyone have a function or mel code snippet that converts a date into yymmdd format?
Thanks
The function below could help you out...I set it up so that it can be expanded over time if you wanted different formats. Disclaimer: Untested, could crash or cause issue.
fn convertDate(d, f)
{
/*
Incoming Parameters:
d: A date, formatted as 01/01/2000
f: How you want your output string to be formatted
Example: convertDate(._TODAYSDATE, "yyyymmdd")
Returns: 20161213
*/
local retStr = ""
local dateArray
cond
case d == "": /*Do Nothing*/
case match(d, 1, "/") <= 0: /*Do Nothing*/
else:
dateArray = getfield(d, "/", "")
if size(dateArray) == 3 then
cond
case f == "yyyymmdd": retStr = dateArray[3] + dateArray[1] + dateArray[2]
endcond
endif
endcond
return retStr
}
That works perfectly!
Thank you very much!
May I ask if you had a way of converting it back into dd/mm/yyyy format?
I was looking at modifying your logic to do it, however I am stuck at the match, when converting from dd/mm/yyyy to yyyymmdd you used the "/" as the deliminator, what would you use for converting it back?
I played around withmaking it a string and adding a deliminator character, however then the mel sort function wont work with it.
Thanks again
RE: Changing it back...
We could use a different function to do that, one that assumes an 8 character string always formatted as YYYYMMDD. In that case, we could do something like below (disclaimer: untested).
fn convertStringToDate(d, f)
{
/*
Incoming Parameters:
d: A date, formatted as YYYYMMDD
f: How you want your output string to be formatted
Example: convertDate(20161213, "mm/dd/yyyy")
Returns: 12/13/2016
*/
local retStr = ""
cond
case d == "": /*Do Nothing*/
case size(d) == 8:
cond
case f == "mm/dd/yyyy": retStr = sub(d, 5, 2) + "/" + sub(d, 7, 2) + "/" + sub(d, 1, 4)
endcond
endcond
return retStr
}