3.6. Day in a Week/Month/Year or Week NumberProblemYou have a date, either in Epoch seconds or as distinct year, month, etc. values. You want 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. Solution
If you have Epoch seconds, the day of the year, day of the month, and day of the week are returned by ($MONTHDAY, $WEEKDAY, $YEARDAY) = (localtime $DATE)[3,6,7]; $WEEKNUM = int($YEARDAY / 7) + 1;
If you have distinct DMYHMS values, you can either convert them to Epoch seconds values as in
Recipe 3.3
and then use the solution above, or else use the use Date::Calc qw(Day_of_Week Week_Number Day_of_Year); # you have $year, $month, and $day # $day is day of month, by definition. $wday = Day_of_Week($year, $month, $day); $wnum = Week_Number($year, $month, $day); $dnum = Day_of_Year($year, $month, $day); Discussion
The use Date::Calc qw(Day_of_Week Week_Number Day_of_Week_to_Text) $year = 1981; $month = 6; # (June) $day = 16; $wday = Day_of_Week($year, $month, $day); print "$month/$day/$year was a ", Day_of_Week_to_Text($wday), "\n"; ## see comment above $wnum = Week_Number($year, $month, $day); print "in the $wnum week.\n";
The governing standards body of particular countries may have rules about when the first week of the year starts. For example, in Norway the first week must have at least 4 days in it (and weeks start on Mondays). If January 1 falls on a week with 3 or fewer days, it is counted as week 52 (or 53) of the previous year. In America, the first Monday of the year is usually the start of the first work-week. Given such rules, you may have to write your own algorithm, or at least look at the See Also
The |
|