FormatDate+function

FormatDate$ function
Following code contains a function to display dates in some particular format (and auxiliary functions which can also be used separately). The function takes a numeric value (days since 01/01/1901, can be obtained from JB's date$ function), and a string describing how the date should be formatted.

The format string can contain any text. Placeholders for date elements begin with "%" and can have the following values:
 * "%d" - day [01-31] with a leading zero
 * "%-d" - day [1-31] without a leading zero
 * "%m" - month [01-12] with a leading zero
 * "%-m" - month [1-12] without a leading zero
 * "%y" - year, 4 digits
 * "%-y" - year, last 2 digits only
 * "%M" - month name in English (e.g January)
 * "%-M" - month name abbreviated (e.g Jan)
 * "%W" - name of the weekday in English (e.g Monday)
 * "%-W" - weekday abbreviation (e.g Mon)
 * "%%" - display per cent symbol (otherwise "%" is simply ignored in the string)

General tips for memorizing the format:
 * lower-case letters indicate numbers, upper-case letters indicate words
 * "%-" (per cent with a minus sign) indicates short form of the following, otherwise a longer form is used.

Example:

code format="qbasic" print FormatDate$(Today, "Today is %W, %M %-d, %y.") code

Output:

code Today is Wednesday, August 4, 2010. code

Source:
code format="basic4gl" FUNCTION Today Today = date$("days") END FUNCTION

FUNCTION FormatDate$( days, format$) for i = 1 to len(format$) if Mid$( format$,i, 1) = "%" THEN select case case Mid$( format$,i + 1, 1) = "m" : FormatDate$ = FormatDate$ + right$("0" + str$(Month( days)), 2) : i = i + 1 case Mid$( format$, i + 1, 2) = "-m" : FormatDate$ = FormatDate$ + str$(Month( days)) : i = i + 2 case Mid$( format$,i + 1, 1) = "d" : FormatDate$ = FormatDate$ + right$("0" + str$(Day( days)), 2) : i = i + 1 case Mid$( format$, i + 1, 2) = "-d" : FormatDate$ = FormatDate$ + str$(Day( days)) : i = i + 2 case Mid$( format$,i + 1, 1) = "y" : FormatDate$ = FormatDate$ + str$(Year( days)) : i = i + 1 case Mid$( format$, i + 1, 2) = "-y" : FormatDate$ = FormatDate$ + right$(str$(Year( days)), 2) : i = i + 2 case Mid$( format$,i + 1, 1) = "M" : FormatDate$ = FormatDate$ + MonthNameFull$( days) : i = i + 1 case Mid$( format$, i + 1, 2) = "-M" : FormatDate$ = FormatDate$ + MonthName$( days) : i = i + 2 case Mid$( format$,i + 1, 1) = "W" : FormatDate$ = FormatDate$ + WeekdayFull$( days) : i = i + 1 case Mid$( format$, i + 1, 2) = "-W" : FormatDate$ = FormatDate$ + Weekday$( days) : i = i + 2 case Mid$( format$,i + 1, 1) = "%" : FormatDate$ = FormatDate$ + "%" : i = i + 1 END select ELSE FormatDate$ = FormatDate$ + Mid$( format$,i, 1) END if   next i END FUNCTION

FUNCTION Weekday$( days) Weekday$ = word$("Tue Wed Thu Fri Sat Sun Mon", days mod 7+1) END FUNCTION

FUNCTION WeekdayFull$(days) WeekdayFull$ = word$("Tuesday Wednesday Thursday Friday Saturday Sunday Monday", days mod 7+1) END FUNCTION

FUNCTION Month( days) d$ = date$(days) Month = val(left$(d$,2)) END FUNCTION

FUNCTION MonthName$( days) MonthName$ = word$("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", Month( days)) END FUNCTION

FUNCTION MonthNameFull$(days) MonthNameFull$ = _ word$("January February March April May June July August September October November December", Month( days)) END FUNCTION

FUNCTION Day( days) d$ = date$(days) Day = val(mid$(d$,4,2)) END FUNCTION

FUNCTION Year( days) d$ = date$(days) Year = val(right$(d$,4)) END FUNCTION

code