Tuesday, July 26, 2011

Calculating working days between two dates with Javascript

Here’s a script that will calculate the number of working days between two Date objects in Javascript.  I had a hard time finding an accurate one online so I wrote one myself – please use with caution as it has not been rigorously tested.
Note that the calculation considers the difference between today and today to equal 1.  I’m using this to present the number of days left before an event, and in my case on the day of the event it makes sense to see “1 day” instead of “0 days.”  
function workingDaysBetweenDates(startDate, endDate) {
    // Validate input
    if (endDate < startDate)
        return 0;
    // Calculate days between dates
    var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
    startDate.setHours(0,0,0,1);  // Start just after midnight
    endDate.setHours(23,59,59,999);  // End just before midnight
    var diff = endDate - startDate;  // Milliseconds between datetime objects    
    var days = Math.ceil(diff / millisecondsPerDay);
    // Subtract two weekend days for every week in between
    var weeks = Math.floor(days / 7);
    days = days - (weeks * 2);

    // Handle special cases
    var startDay = startDate.getDay();
    var endDay = endDate.getDay();
    // Remove weekend not previously removed.   
    if (startDay - endDay > 1)         
        days = days - 2;      
    // Remove start day if span starts on Sunday but ends before Saturday
    if (startDay == 0 && endDay != 6)
        days = days - 1  
    // Remove end day if span ends on Saturday but starts after Sunday
    if (endDay == 6 && startDay != 0)
        days = days - 1  
    return days;


Thursday, May 19, 2011

If I have to tell you what to do...

The recent This Developer's Life podcast features the StackOverflow team discussing pressure and how they dealt with the site's unexpected downtime in October 2010. About 31 minutes in, Scott Hanselman interviews Jeff Atwood, and Jeff revealed an eye-opening insight into how he runs his company. Speaking about his team, he remarks:
If I have to tell you what to do...you suck.
He prefaces that statement with an assurance that it's tongue-in-cheek. But there is truth behind that statement. He goes on to say he "unleashes" his team, and expects them to come to him with ideas that he can "simply sign-off on." He maintains the vision and keeps them moving in the correct direction. Unlike a traditional work environment where the boss tells the subordinate what to do, here the team's drive comes from the bottom-up.
I found this fascinating. I want to work that way. Being a solo-developer at my company, I play both roles to a degree, setting the direction for a project while trying to come up with creative ideas to enhance it. But working alone it is hard to validate whether the way I work would fit within a team environment. It is really motivating to have this insight on how this successful company (for which I have a ton of respect) operates.