Recipe 3.10. Finding the Day of the WeekProblemYou want to find the day of the week for a certain date. SolutionUse the The following code yields to a code block the date of every Sunday between two dates. It uses
DiscussionThe most commonly used parts of a time are its calendar and clock readings: year, day, hour, and so on. Time and DateTime let you access these, but they also give you access to a few other aspects of a time: the Julian The every_sunday method will accept either two Time objects or two DateTime objects. The only difference is the number you need to add to an object to increment it by one day. If you're only going to be using one kind of object, you can simplify the code a little. To get the day of the week as an English string, use the strftime directives %A and %a:
You can find the day of the week and the day of the year, but Ruby has no built-in method for finding the week of the year (there is a method to find the commercial week of the year; see Recipe 3.11). If you need such a method, it's not hard to create one using the day of the year and the day of the week. This code defines a week method in a module, which it mixes in to both Date and Time:
See Also
|
Monday, October 26, 2009
Recipe 3.10. Finding the Day of the Week
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment