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);
var 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.

Wednesday, September 29, 2010

Getting Twitter’s @Anywhere hovercards to work based on URLs

Twitter has a cool script you can add to your site to turn any references to a twitter account into a helpful hovercard that shows the twitter user, some stats, and a Follow button.  It’s subtle and useful, but it could be a little easier to add for developers.

I won’t repeat what is already available on the docs at Twitter, but the gist is if I put @kpespisa in a post, the @anywhere script will find that and turn it into a hyperlink that shows a hovercard when you put your mouse over the link. 

Sadly I noticed there was no simple way to get a hovercard to appear based on the URL of a hyperlink.  If I added a link that says “Follow me on twitter”, it would be nice to have the hovercard appear when I hover over that hyperlink as well. 

Adding a little jQuery gets the job done.  Using this code, you can just set the URL to http://twitter.com/(twitteraccount) and it will create a hovercard for you using the twitter account that is in the URL.

   1: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
   2: <script src="http://platform.twitter.com/anywhere.js?id=AMyKqypXu1r6qmOvc5OZCQ&amp;v=1"></script>
   3: <script type="text/javascript">
   4:     $(document).ready(function() {
   5:         $("a[href^='http://twitter.com']").add("a[href^='http://www.twitter.com']").attr("title", function() {
   6:             return '@' + this.href.substr(this.href.lastIndexOf('/') + 1);
   7:         });
   8:     });
   9:  
  10:     twttr.anywhere(function(T) { 
  11:         T.hovercards(); 
  12:         
  13:         T("a[title^='@']").hovercards({
  14:             username: function(node) {
  15:                 return node.title;
  16:             }
  17:         });
  18:     });
  19: </script>

For example:  Follow me on Twitter!

Thursday, May 06, 2010

DotNetRocks in Boston


This past Monday I went to see the Boston stop of the DotNetRocks road trip, hosted by Carl Franklin and Richard Campbell. The duo put on a good event, bringing in guest Chris Sells for their podcast, as well as a panel of guests to discuss F#.

The portion of the evening I found most interesting was the F# discussion. I didn't expect to get much out of it. I had heard enough of F# to that point to make up my mind that it wasn't for me. I reasoned that if it isn't a language I can completely switch to, I'm better off focusing on VB or C#.

So I was surprised when I heard the F# experts suggest that we use the language to complement our projects rather than use it as an all-purpose language. They encouraged us to use the language for its strengths, such as asynchronous processing and parallelism. One could create an assembly written in F# and include that in a VB project, for example, to handle some heavy computations. We already use a handful of non-core languages in our projects such as HTML, Javascript, LINQ or SQL, so the idea of blending yet another language into our projects is not a foreign concept.

The panel suggested we add F# to our repertoire and understand its strengths, and use it where it's beneficial. I'm going to take that advice. Bottom line is if I can replace 50 lines of confusing C# code with 10 lines of elegant F#, I've done myself a service.

A big thank you to Carl and Richard for their hard work in putting this event together, and for entertaining me with their podcasts during my commutes, jogs, walks, and housework.

Tuesday, May 04, 2010

Setting up a Mercurial server on Win2k3 / IIS 6.0

There are several tutorials right now explaining how to setup a Mercurial server in Windows, but most are outdated just enough to make the whole process fail due to recent changes to Mercurial.  I tried a few, but the best one available as of Mercurial 1.5.2 is from Matt Hawley from eWorldUI.net.  His post explains how to set up the server using IIS 7.0, and the process is slightly different in IIS 6.0, as I’ll explain.

First, read his blog post to get a sense of the procedure.  The post is broken into two major sections, Packages Installation, and Configuration.  I followed the Packages Installation section exactly as written, with the exception of downloading the latest source for Mercurial, which is 1.5.2. 

Once you’ve completed the Packages Installation, follow these steps instead to configure IIS 6.0.  Many of these steps are the same as Matt describes, but for convenience I’ve duplicated them here.  All credit for this goes to Matt for getting us 99% of the way there. 

These steps assume you have IIS installed already.

  1. Create a folder to host your Mercurial site.  As Matt suggests, you can use c:\inetpub\hg
  2. Copy the hgwebdir_wsgi.py file from c:\(mercurial source code location)\contrib\win32 to c:\inetpub\hg
  3. Edit hgwebdir_wsgi.py in a text editor and change the two lines as follows:

    hgweb_config = r’c:\inetpub\hg\hgweb.config’
    path_prefix = 0
  4. Open a command prompt and go to the c:\inetpub\hg folder
  5. Run python hgwebdir_wsgi.py.  If python isn’t installed on the system path, you may need to include the path to it and use c:\python26\python hgwebdir_wsgi.py
  6. Create a new text file named hgweb.config in the c:\inetpub\hg folder and add the following.  Here c:\repos is the folder where you will store your repositories.

    [paths]
    / = c:\repos\*
     

    At this point, your folder should look like this:
    image
  7. In IIS Manager, create a new application pool with the name Mercurial.
    image
  8. Create a new website called Mercurial, and set the TCP port to 81 (or any unused port).  Leave the Host header field blank.
    image
  9. Set the path to c:\inetpub\hg and finish the wizard.
    image
  10. Right-click the Mercurial website and choose properties.
  11. On the Home Directory tab, click Configuration, and then click Insert… in the Wildcard application maps section.  Enter the path to the isapi dll (c:\inetpub\hg\_hgwebdir_wsgi.dll) and uncheck verify file exists.
    image
  12. On the ISAPI filters tab, click Add… and enter Mercurial-ISAPI as the filter name and c:\inetpub\hg\_hgwebdir_wsgi.dll as the executable.
    image
  13. Finally, go to the Web Service Extensions in IIS Manager and Add a new Web service extension.  Give it the name Mercurial-ISAPI, and add the required file c:\inetpub\hg\_hgwebdir_wsgi.dll, and check the “Set extension status to Allowed” option.
    image
  14. Finally, browse to http://localhost:81 (or substitute 81 for the port you chose in step 8).  You should now see the Mercurial Repositories page!

    image

Wednesday, February 17, 2010

iPhone Game Rebates

Jon Lam, CEO of the game development company Ph03nix New Media, has started a new program to push iPhone games to the top of the App Store charts. The site, iPhoneGameRebates.com, will highlight a selected app for two weeks, offering a full rebate (up to $1) if you purchase that app and forward your iTunes receipt.
For a limited time, to build up a following, they are offering this program on any app in the App Store. The site is legitimate and I have already received my rebate, which was sent directly to my PayPal account.
How does this work? Essentially the app developers cover the cost of the rebate (meaning they lose the 30% commission taken by Apple), and in return their apps are pushed up the charts. By climbing the charts, the app is more exposed and more people are likely to buy it, thus covering the loss taken during the rebate period.
I'm definitely going to be a fan of this site, and can't wait to see what apps they give away starting next month!

Wednesday, September 16, 2009

10 must-have iPhone apps according to Ken

These are my favorites as of right now.  At this moment.  Subject to change at any time without notice.

I left off the social media apps everyone has, like Facebook, LinkedIn, and TweetDeck.  I also excluded the apps I’ve written to eliminate the obvious bias :) 

Here are 10 apps I use on a regular basis:

 #1 Simplify Media 2 ($5.99)

Simplify Media lets you stream music from your computer to your iPhone.  The downside is you need to leave your computer on.  But if you do that anyway then you can have your entire music collection with you anywhere you go.  For me, that’s enough music to fill 10 iPhones.  Plus I don’t have to sync music to my phone.  Once it is on my computer I can play it on my phone.  And the sound quality is great.

 #2  i.TV 2.0 (Free)

i.TV is the perfect TV guide app, especially if you have a TiVo.  You can see TV listings in a stylish format and flip through stations and show times quickly.  Once you find a show, you can read the synopsis, tell your TiVo to record it, and even change to the station using a built in TiVo remote (yes, control your TiVo from your iPhone.  Is there anything the iPhone can’t do?)  There’s tons more to this app that I haven’t yet explored.

 

 #3 Evernote (Free)

Evernote keeps Text, Pictures, and Voice notes, and does so with a nice easy-to-use inteface.  The real exciting feature, though, is all of your notes are synced and available via the Evernote Web site too.  I jump between using the Evernote Web site at my desk, and the iPhone app on the go.

 

 #4 Lose It! (Free)

Lose it! is designed to help you control your weight, the only way that works – by counting calories.  It has a huge, though not complete, list of foods including store brands and popular restaurant chains.  For the foods it is missing, you can add custom foods or recipes.  It tracks all sorts of exercises too.  You start by setting a goal, then as you track intake and exercise, you can see exactly how well you are doing.  I’ve only just started using it, but it does help you think twice before grabbing that brownie in the cafe (not that it stops me, mind you!)

 #5 Zenbe Lists ($2.99)

Zenbe is a simple app for keeping to-do lists, shopping lists, etc.  It syncs with a free Web site so you can update lists in both places.  I use it a lot for grocery shopping and keeping a list of projects I need to do at home.  There are many to-do lists in the App Store, including ones that are made specifically for grocery shopping, but I find this simple app works best for me. 

 

 #6 Sportacular (Free)

Sportacular has all the details you need for a sports fan who needs to know the current score of a game, or a team’s upcoming schedule, etc.  It’s a well-designed app and covers all the bases, if you will :)  Gives you play-by-plays, stats, and even has an alert feature to notify you when a score has changed.

 

 #7 Let’s Golf ($1.99)

I haven’t purchased many games for the iPhone, but this one caught my attention and is pretty amazing for the price.  I got it on sale for a buck, and it was worth every penny.  Very addictive once you get the hang of it.

 

 #8 Pandora Radio (Free)

Pandora is a free music streaming application that generates playlists based on a band name you enter.  The app uses a highly analytical process to guess what other music you’d like based on the band you select.  It is usually spot on.  This app was one of the reasons I dumped my Sirius radio subscription.  Who needs a monthly fee when I can get this for free?

 

 #9 Shazam (Free)

Have you ever heard a song and wished you could push a button to find out who the artist is, and what the name of the song is?  Shazam does that for you in 12 seconds.  Just hit “Tag Now”, hold your iPhone near the speaker, and it finds the artist, song, lyrics, bio, and much more.  It is worth checking out just for the pure novelty of it. 

 

 #10 iMapMyRun (Free)

Here’s another app that has replaced a separate device I used to use.  iMapMyRun will track your running routes using the GPS on the iPhone.  It records time, distance, pace, etc, and syncs that information to mapmyrun.com, where you can enter way more information about your run than you’d ever care to.  They also have an app for bikers and other activities.  It’s the best run-tracking app out there at the moment.  And I don’t need my giant Garmin wrist watch anymore to track my distances.

 

So there’s my list.  What are your favorites?  Feel free to comment!