Week of the Year and Day of the Week Month Year
From Erlang Community
| Revision as of 03:32, 24 November 2006 (edit) Kaiserpanda (Talk | contribs) m ← Previous diff |
Current revision (16:05, 20 March 2011) (edit) (undo) Imrehorvath (Talk | contribs) |
||
| (7 intermediate revisions not shown.) | |||
| Line 5: | Line 5: | ||
| == Solution == | == Solution == | ||
| - | + | These functionality are available using the calendar module, directly or indirectly. | |
| <code> | <code> | ||
| Line 27: | Line 27: | ||
| </code> | </code> | ||
| - | Unfortunately, there | + | Unfortunately, there is no built-in function for identifying the day of the year (directly). However, by doing a bit of math we can figure it out: |
| <code> | <code> | ||
| 7> Day0 = calendar:date_to_gregorian_days(2004,1,1). | 7> Day0 = calendar:date_to_gregorian_days(2004,1,1). | ||
| Line 36: | Line 36: | ||
| 118 | 118 | ||
| </code> | </code> | ||
| - | + | ||
| + | There are different week of the year definitions used by different parts of the world. The calendar module contains the function for determining the week number which conforms to the ISO 8601 standard. (Available in Erlang version R14B02.) | ||
| + | <code> | ||
| + | 1> calendar:iso_week_number(). | ||
| + | {2011,11} | ||
| + | 2> calendar:iso_week_number({2006,1,1}). | ||
| + | {2005,52} | ||
| + | 3> calendar:iso_week_number({2007,1,1}). | ||
| + | {2007,1} | ||
| + | 4> calendar:iso_week_number({2008,12,31}). | ||
| + | {2009,1} | ||
| + | </code> | ||
| + | The function returns a 2 tuple with the year and the iso week number. It returns the year because the date can fall on the previous or the next year too! | ||
| + | |||
| + | One very useful thing (beloved of Financial Systems Programmers everywhere) is that Erlang has an end-of-month function: | ||
| <code> | <code> | ||
| Line 51: | Line 65: | ||
| [[Category:CookBook]][[Category:DateTimeRecipes]] | [[Category:CookBook]][[Category:DateTimeRecipes]] | ||
| - | |||
| - | |||
| - | |||
| - | [http://www.3wcasinos.com/roulette-tips/roulette-description.html roulette description] | ||
| - | [http://www.casino-theory.com/bingo-online/gambling-online-bingo.html gambling online bingo] | ||
| - | [http://www.3wcasinos.com/roulette-tips/index.html roulette tips] | ||
| - | [http://www.casino-theory.com/online-casino-bonus/index.html online casino bonus] | ||
| - | [http://www.slots-wiki.com/index.php/slots_tips slots tips] | ||
| - | [http://www.gambling-online-theory.com/online-casino/online-casino-for-cash.html online casino for cash] | ||
| - | [http://www.magical-casino.com/online_games.html Online casino games.] | ||
| - | [http://www.magical-casino.com/strategy.html Online casino strategies.] | ||
| - | [http://www.gambling-online-theory.com/casinos-portal/internet-casinos-sites.html internet casinos sites] | ||
| - | [http://www.casinos-go.com/online-casino-tips/online-casino-on-the-net.html online casino on the net] | ||
Current revision
[edit] Problem
Given a date, you need to find out what week of the year, day of the week, day of the month, or day of the year that the date falls on.
[edit] Solution
These functionality are available using the calendar module, directly or indirectly.
1> {Today,Time} = erlang:universaltime().
{{2004,8,28},{7,59,8}}
2> DayOfWeek = calendar:day_of_the_week(Today).
6
3> {_, Month, DayOfMonth} = Today.
{2004,8,28}
4> DayOfMonth.
28
|
If you wanted to get the text string representing the day or the month, you can use the httpd_util module (which contains all sorts of useful things that should be moved to the calendar and string modules:
5> DayName = httpd_util:day(DayOfWeek). "Sat" 6> MonthName = httpd_util:month(Month). "Aug" |
Unfortunately, there is no built-in function for identifying the day of the year (directly). However, by doing a bit of math we can figure it out:
7> Day0 = calendar:date_to_gregorian_days(2004,1,1). 731946 8> TodayDay = calendar:date_to_gregorian_days(2004,Month,DayOfMonth). 732064 9> DayOfYear = TodayDay - Day0. 118 |
There are different week of the year definitions used by different parts of the world. The calendar module contains the function for determining the week number which conforms to the ISO 8601 standard. (Available in Erlang version R14B02.)
1> calendar:iso_week_number().
{2011,11}
2> calendar:iso_week_number({2006,1,1}).
{2005,52}
3> calendar:iso_week_number({2007,1,1}).
{2007,1}
4> calendar:iso_week_number({2008,12,31}).
{2009,1}
|
The function returns a 2 tuple with the year and the iso week number. It returns the year because the date can fall on the previous or the next year too!
One very useful thing (beloved of Financial Systems Programmers everywhere) is that Erlang has an end-of-month function:
10> LastDayOfMonth = calendar:last_day_of_the_month(2004, 8). 31 11> calendar:last_day_of_the_month(2004,2). 29 12> calendar:last_day_of_the_month(2005,2). 28 |
If you are wondering what to use when you are using the Epoch (seconds) representation of the given date, then you should consider transforming your number of seconds to date, and then use this recipe. See recipe TimeEpochToTime to pass from seconds to dates.

Digg It
Del.icio.us
Reddit
Facebook
Stumble Upon
Technorati

