Date to String Formatting

Prev Next

Introduction

Some powerful formatting features are available for formatting dates, possibly combined with time of day information in any combinations. The formatting template applies to following functions:

Formatting Templates

Formatting Template Description Example Note
D Day, 1 or 2 digits 1 .. 31 See 1
DD Day, 2 digits 01 ... 31 See 1
DDD / Ddd / ddd Day, 1 or 2 digits, followd by ordinal 1ST ... 31ST / 1St ... 31St / 1st ... 31st See 2
DDDD / Dddd / dddd Day, 2 digits, followd by ordinal 01ST ... 31ST / 01St ... 31St / 01st ... 31st See 2,3
M Month, 1 or 2 digits 1 .. 12 See 1
MM Month, 2 digits 01 ... 12 See 1
MMM / Mmm / mmm Month name, abbreviated JAN / Jan / jan See 2,3
MMM / Mmm / mmm Month name JANUARY / January / january See 2,3
Q Quarter, 1 digit 1 ... 4 See 1
QQ / qq Quarter, 1 digit after Q Q1 ... Q4 / q1 ... q4 See 4
QQQ / qqq Quarter, 1 digit before Q 1Q ... 4Q / 1q ... 4q See 4
T Weekday number 1 (Monday) .. 7 (Sunday) See 1
TT / Tt / tt Weekday name, abbreviated (typ. 2 chars) MO ... SU / Mo ... Su / mo ... su See 2,3
TTT / Ttt / ttt Weekday name, abbreviated (typ. 3 chars) MON...SUN / Mon...Sun / mon...sun See 2,3
TTTT / Tttt / tttt Weekday name MONDAY / Monday / monday See 2,3
W ISO week number, 1 or 2 digits 1 .. 53 See 1,5
WW ISO week number, 2 digits 01 .. 53 See 1,5
WWW / www ISO week number, 1 letter + 1 or 2 digits W1 .. W53 / w1 .. w53 See 3,4,5
WWWW / wwww ISO week number, 1 letter + 2 digits W01 .. W53 / w01 .. w53 See 3,4,5
WWWWW / Wwwww / wwwww ISO week number, abbreviation + 1 or 2 digits WK 1 / Wk 1 / wk 1 See 2,3,5
WWWWWW / Wwwwww / wwwwww ISO week number, abbreviation + 2 digits WK 01 / Wk 01 / wk 01 See 2,3,5
WWWWWWW / Wwwwwww / wwwwwww ISO week number, week name + 1 or 2 digits WEEK 1 / Week 1 / week 1 See 2,3,5
WWWWWWWW / Wwwwwwww / wwwwwwww ISO week number, week name + 2 digits WEEK 01 / Week 01 / week 01 See 2,3,5
X Adjusted week number, 1 or 2 digits 0 .. 53 See 1,6
XX Adjusted week number, 2 digits 00 .. 53 See 1,6
XXX / xxx Adjusted week number, 1 letter + 1 or 2 digits W0 .. W53 / w1 .. w53 See 3,4,6
XXXX / xxxx Adjusted week number, 1 letter + 2 digits W01 .. W53 / w01 .. w53 See 3,4,6
XXXXX / Xxxxx / xxxxx Adjusted week number, abbreviation + 1 or 2 digits WK 00 / Wk 00 / wk 00 See 2,3,6
XXXXXX / Xxxxxx / xxxxxx Adjusted week number, abbreviation + 2 digits WK 00 / Wk 00 / wk 00 See 2,3,6
XXXXXXX / Xxxxxxx / xxxxxx Adjusted week number, week name + 1 or 2 digits WEEK 0 / Week 0 / week 0 See 2,3,6
XXXXXXXX / Xxxxxxxx / xxxxxxx Adjusted week number, week name + 2 digits WEEK 00 / Week 00 / week 00 See 2,3,6
Y and YY Year, 2 digits 20 See 1
YYY and YYYY Year, 4 digits 2020 See 1
Z and ZZ Adjusted Year, 2 digits, use with ISO week number E.g. str( date("2019-12-31"), "YY-WW" -> 20-01 See 1,7
ZZZ and ZZZZ Adjusted Year, 4 digits, use with ISO week number E.g. str( date("2019-12-31"), "YYYY-WW" -> 2020-01 See 1,7
H Hour, 1 or 2 digits, 0 - 23 range 0 ... 23
h Hour, 1 or 2 digits, 1 - 12 range 1 ... 12
HH Hour, 2 digits, 0 - 23 range 00 ... 23
hh Hour, 2 digits, 1 - 12 range 01 ... 12 See 8
I Minute, 1 or 2 digits 0 ... 59 See 1
II Minute, 2 digits 00 ... 59 See 1
S Second, 1 or 2 digits 0 ... 59 See 1
SS Second, 2 digits 00 ... 59 See 1
A / a a.m. / p.m. info, 1 letter A, P / a, p
AA / Aa / aa a.m. / p.m. info, 2 letters AM, PM / Am, Pm / am, pm
AAA / Aaa / aaa a.m. / p.m. info, 4 letters A.M., P.M. / A.m., P.m. / a.m., p.m. See 2
' ... ' Contents inside quotation marks are treated as given text, including spaces "o'clock" See 9
" ... " See above 'time: ' See 10
All other characters Treated as given text

1 Template letter is case insensitive. E.g. 'DD', 'Dd', 'dD' and 'dd' make no difference.
2 Case of 1st letter determines 1st letter in the output, case of 2nd letter determines case of all remaining letters. The cases of following letters (if 3 or more consecutive letters specified) are ignored.
3 Uses the language as confgured by locale parameter or setting.
4 Case of 1st letter determines 1st letter in the output. The cases of following letters (if 2 or more consecutive letters specified) are ignored.
5 ISO Week numbers: In some years, the first day lies in week 53 of the previous year. In some other years, the last day lies in week 1 of the following year.
6 Adjusted week numbers: In some years, the first day lies in week 0. Last day in all years are either week 52 or 53, but not 0.
7 Referring to 5, These are the matching year numbers adjusted to the ISO week numbers.
8 Case of 1st letter determines the output format. The cases in the following consecutive letters are ignored.
9 Applicable outside double quotation marks, otherwise symbols are treated as given text
10 Applicable outside single quotation marks, otherwise symbols are treated as given text

Locales

Specify a supported locale name, a country name or a language name. See locales for an overview of supported locales. Choices are not case sensitive.

Examples: en_US, DE_at, France, German, local, none

Program Examples without formatting templates

Following examples show use of date to string conversion without formatting template

               date 1[] = date("2020-07-04"); // Some national holidays
               date 2[] = date("2020-10-03");
               date 3[] = date("2020-07-14");
               date 4[] = date("2020-08-01 22:30:00");
               date 5[] = date("22:30:00");

               echo( str( date 1[], " *", EN_us ));
               echo( str( date 2[], "*", EN_de ));
               echo( str( date 3[], "*", France ));
               echo( str( date 4[], "*", de_CH ));
               echo( str( date 3[], "*", none ));
               echo( str( date 2[], "*", local ));
               echo( str( date 1[] )); // assumes '*' and none
               echo( str( date 5[], "*", EN_us )); // Always 0-23h if not specified otherwise
 *
03/10/2020
14/07/2020
01.08.2020 22:30:00
2020-07-14
03.10.2020
2020-07-04
22:30:00
Try it yourself: Open LIB_Features_date_to_string_conversion_and_formatting.b4p in B4P_Examples.zip. Decompress before use.

Program Examples with formatting templates

Following examples show use of date to string conversion with formatting template

               date 1[] = date("2020-07-04"); // Some national holidays
               date 2[] = date("2020-08-01 22:30:00");

               echo( str( date 1[], "Tttt DD.Mmmm.YY", EN_us ));
               echo( str( date 2[], "Ttt DD.mm.YYYY ' Zeit: ' HH:ii:ss", DE_de ));
               echo( str( date 2[], "Ttt DD.mm.YYYY ' Zeit: ' HH:ii:ss", DE_de ));
               echo( str( date 2[], "Ttt MM/DD/YY ' Time: ' hh:ii:ss aaa", en_US ));
               echo( str( date 1[], "Tttt DD.Mmmm.YY", Hindi ));

               echo( new line, "Show difference between ISO and adjusted week and year numbers: ");
               echo( str( date( "2019-12-31"), "'Week: 'ZZZZ-WW 'and' YYYY-XX") ); // Different
               echo( str( date( "2020-06-30"), "'Week: 'ZZZZ-WW 'and' YYYY-XX") ); // Same
               echo( str( date( "2021-01-01"), "'Week: 'ZZZZ-WW 'and' YYYY-XX") ); // Different
Saturday 04.July.20
Sam 01.08.2020  Zeit:  22:30:00
Sam 01.08.2020  Zeit:  22:30:00
Sat 08/01/20  Time:  10:30:00 p.m.
शनिवार 04.जुलाई.20

Show difference between ISO and adjusted week and year numbers:
Week: 2020-01 and 2019-53
Week: 2020-27 and 2020-27
Week: 2020-53 and 2021-00
Try it yourself: Open LIB_Features_date_to_string_conversion_and_formatting_01.b4p in B4P_Examples.zip. Decompress before use.