Birds-Eye.Net
All things broadband and more...
 
Web Birds-Eye.Net

What's New?

Ruby on Rails (RoR)
Programming Reference


Models
External database connections
Passing current_user into model
Passing object into model
Using static lookup tables
Validates IF
Validates MongoMapper

Views
Dynamically delete form element
Edit create nested data
HTML form field check_box
Layout jQuery datatable module
Select array
Select cascading via JS
Text_area Array
Text_area listing submit
Text field format time

Controllers
Dynamic model selection
Including first item from a sorted desc table
Using from_unixtime on epoch dates

Rack
Integrated NTLM/Kerberos Authentication
Pass-through Authentication w/ NTLM

ActionMailer
Broken links in emails

Rails General
Add, Subtract, Multiply, and Divide
Calculate number of weekdays for date range
Date->Epoch & Epoch->Date
Extract first letter of each word
Hash of hashes assignment
Using: variable as hash index

jQuery
jQuery accordion MongoDB


More to come

 

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.