The javascript Date object and how to add days to a date variable

JavaScript is not an object oriented language, it’s a prototype based language. This means that you can extend any existing object by just writing a new function for it on-the-fly. Here’s an example:

// add n number of days
Date.prototype.goto = function(n) {
	this.setDate(this.getDate()+n*86400000);
	return this;
};



Note: Thanks to commenter Joe I use setDate instead of setTime.
Yes I know… GOTO is considered harmful. But so is Edsger Dijkstra. Anyway, this nifty little function will add, or subtract(!), any number of days with an easy to use syntax:

// add n number of days
d = new Date; // today
d.goto(7); // move it to next week
d.goto(-365); // move to last year (well if it isn't leap)
};

Try it yourself, it’s fun. Now to make things more interesting, you can make a generic function (erm.. method?!) that will calculate the number of days since the Epoch

Date.prototype.days = function() { return parseInt(this.getTime() / (1000*24*60*60)); };

This makes stuff like comparing dates and calculating differences way easier. E.g. check out this one for calculating the number of working days between two dates:

Date.prototype.workdays = function(d2) {
	var start = new Date(this);
	var end = d2;
	var ret = 0;
	var diff = end.days() - start.days();
	if (diff > 0)
		// up to 1 year
		while (start.days() < end.days() && ret < 3650)
		{
			start.goto(1); // move to next day
			if (start.getDay() != 0 && start.getDay() != 6)
				ret++;
		}
	return ret;
};
Advertisements

Yes, we can!

Yes We Can – Barack Obama Music Video


Normally I don’t post videos on my blog, because it’s done too much already, but I though this one was appropriate to share with as many as possible. It’s not just the lyrics (eh. sorry speech), it’s also a new concept in music…

Yahoo, winner or loser?

If it’s up to Michael Arrington, Yahoo loses, but it’s not clear why. Yes, stock is going down, but that doesn’t define them. Just like your wallet is not your personality. Yes, Jerry Yang looks like a nerd, but so does Bill Gates.

If they go under they fail, of course, but how could they, having been nr 1 on the net for so long… There will always be advertisement money then, it’s simply a matter of downsizing and waiting for better times.

I am almost always impressed by the services of Yahoo, as delicious and even Yahoo Photos (deceased). It’s too bad that they don’t get what they deserve. Google is becoming like MS, they do great things, but always a little too late. Yahoo had autocomplete on their search a looooong time before Google. But when google delivers Google Suggest it’s suddenly something ‘new’. All eyes are on Google, but Yahoo technology stands already. Google doesn’t have BOSS, or YDN, or YUI. Google code is usually a bunch of spaghettiballs, so smart it doesn’t understand itself anymore. Yahoo code is clean, organized.

But inertia happens. It’s hard for such a big company to get rolling in a different direction. At least I hope they stop pursuing the social network dream. Let it go to Facebook and try to improve search this time, for real. Why don’t Yahoo index the dark matter in the web? There are still many new ideas to be tried which can result in a big success. Google is betting everything on keyword search, Microsoft is already in the natural language game (with Powerset), but real indexing done right hasn’t been done yet.

How about the interface? I need to do ‘hum hum hum’ and get the song title on my screen. Like that. I need to be able to sketch a flower and get the word ‘flower’ on my screen. And instead of ‘did you mean’ I need navigtion through association. By the way, I believe it was Excite that had that functionality a decade ago. You would type ‘Gibson’ and it would come up with ‘Mel Gibson (person)’ – ‘Gibson – guitar’, etc. It can’t be that hard if it was already possible ten years a go.

Yahoo, you have my vote.

Open source project management – learn for free

I had heard about open source software, but it didn’t occur to me other stuff could be open source as well. A friend of mine published what was my first acquaintance with an open source book. At http://www.projectmanagement-training.net/ you can learn a lot about project management, and if you’re a project manager yourself you may want to add to it. You can also opt for a training, as the domain name most blatantly suggests.

I must shamelessly admit I have merely scanned over it, and haven’t added anything, although I’ve been a trainer (e.g. MS Project), a project manager, and a writer for quite a while.

download the book