Erlang Central

Todays Date

From ErlangCentral Wiki


You need to find the year, month, and day values for today's date.


Use erlang:now(), and now_to_datetime to get a date structure type representing today.

1> {{Year,Month,Day},_} = calendar:now_to_datetime(erlang:now()).

Using the io or io_lib module, you can easily convert a date structure to a string.

% Note:  This example uses the above form to bind Year, Month, and Day
2> io:fwrite("Today's Date is ~2B/~2B/~4B\n",
2>       [Month, Day, Year]).
Today's Date is  8/27/2004

We can get fancier and show the time as well.

3> {{Year,Month,Day},{Hour,Min,Sec} = 
3>    calendar:now_to_datetime(erlang:now()).
4> io:fwrite("Today's Date is ~2B/~2B/~4B ~2B:~2.10.0B:~2.10.0B\n",
4> [Month, Day, Year, Hour, Min, Sec]).                            
Today's Date is  8/27/2004 23:59:06

Note the use of the three-part B format specification. The first part is the number of digits to show (2), the second is the base of the number (decimal in this case), and the third is a character to fill any single-digit numbers with (in this case, we want leading zeros).

Ideally, it would be nice if a few convenience functions were available to provide locale-specific date and time formats, but these do not seem to be present in Erlang at this time.