Logo

Programming-Idioms

  • Erlang
  • Groovy
  • Smalltalk

Idiom #99 Format date YYYY-MM-DD

Assign to the string x the value of the fields (year, month, day) of the date d, in format YYYY-MM-DD.

x := d yyyymmdd.
D = erlang:localtime(),
{{Year, Month, Day}, {_Hour, _Minute, _Second}} = D,
X = lists:flatten(io_lib:format("~4..0w-~2..0w-~2..0w", [Year, Month, Day])).  % "2017-07-02"
with Ada.Calendar.Formatting;
X : constant String :=
    Ada.Calendar.Formatting.Image (D) (1 .. 10);

The Image function returns time as well therefore the slice.

New implementation...