Formatdate = function (date, format ){
Function stringrepeat (CHR, count ){
VaR STR = "";
For (VAR x = 0; x <count; X ++) {STR + = CHR };
Return STR;
}
Function stringpadl (S, width, pad ){
If (! Width | width <1)
Return S;
If (! PAD) pad = "";
VaR length = width-S. Length
If (length <1) return S. substr (0, width );
Return (stringrepeat (pad, length) + S). substr (0, width );
}
Function stringpadr (S, width, pad ){
If (! Width | width <1)
Return S;
If (! PAD) pad = "";
VaR length = width-S. length;
If (length <1) S. substr (0, width );
Return (S + stringrepeat (pad, length). substr (0, width );
}
If (! Format)
Format = "mm/DD/YYYY ";
VaR month = date. getmonth () + 1;
VaR year = date. getfullyear ();
Format = format. Replace ("mm", stringpadl (month. tostring (), 2, "0 "));
If (format. indexof ("YYYY")>-1)
Format = format. Replace ("YYYY", year. tostring ());
Else if (format. indexof ("YY")>-1)
Format = format. Replace ("YY", year. tostring (). substr (2, 2 ));
Format = format. Replace ("DD", stringpadl (date. getdate (). tostring (), 2, "0 "));
VaR hours = date. gethours ();
If (format. indexof ("T")>-1 ){
If (hours> 11)
Format = format. Replace ("T", "PM ")
Else
Format = format. Replace ("T", "am ")
}
If (format. indexof ("hh")>-1)
Format = format. Replace ("hh", stringpadl (hours. tostring (), 2, "0 "));
If (format. indexof ("hh")>-1 ){
If (hours> 12) hours-12;
If (hours = 0) hours = 12;
Format = format. Replace ("hh", stringpadl (hours. tostring (), 2, "0 "));
}
If (format. indexof ("mm")>-1)
Format = format. Replace ("mm", stringpadl (date. getminutes (). tostring (), 2, "0 "));
If (format. indexof ("SS")>-1)
Format = format. Replace ("SS", stringpadl (date. getseconds (). tostring (), 2, "0 "));
Return format;
};