|
Calculate Number of Weekdays for Date Range
By: Bruce Bahlmann - Contributing Author (your
feedback
is important to us!)
In rails development, you may have an occasion where you need to determine
the number of weekdays (mon, tues, wed, thur, fri) for a specified date
range. In this case, you mainly care about the number of weekdays that fall
within that date range so you are looking for simply a number of weekdays
expressed as an integer that occur within a date range you specify.
If you attempt to solve using rails, there is no direct way to arrive at this
number. Instead, you need to calculate it. However there are some
convenience resources (e.g. date) that provide most of the supported
functionality you will need.
[apps/controllers/timeinout_controller.rb]
require 'date'
startDate = <some start date>
endDate = <some end date (after above start date>
# Calculate the number of weekdays since date
@wdays = ( (startDate)..(endDate) ).select {|d| (1..5).include?(d.wday) }.size
In the above example, we require the date resource and must define a start
date (startDate) and end date (endDate). Given this information, we generate
an instance variable (@wdays) which represents an integer indicating the
number of days between the start and end dates which are weekdays. Weekdays
are particularly useful in determining things like PTO accumulation,
employee payroll, etc. Essentially, weekdays are workdays, so its a useful
routine be able to reliably generate this information.
Can Birds-Eye.Net help you or your Company?
Receive your Birds-Eye.Net articles and white
papers hot off
the presses by adding our RSS feed to your reader.
|
|
|
(C) Copyright Birds-Eye.Net, All rights reserved.
It is against the law to reproduce this content or any portion of it in any form without the explicit written permission of Birds-Eye Network Services, LLC. Federal copyright law (17 USC 504) makes it illegal, punishable with fines up to $100,000 per violation plus attorney's fees.
|