How to get the current date with javascript
2 min read

How to get the current date with javascript

I've dealt with error logging in javascript before, but this time I needed a simple logging function, with a timestamp. There were some scroll events triggered and the lack of a timestamp in the Google Chrome's console. The logging part was easy, everybody knows how to use console.log. The timestamp bit, not so much...

First thing I did was to use good ol' Google and I ended up on Stackoverflow. There was a nice answer there that contained everything I wanted so with a little adjustment, I cropped this function:

function log(msg) {
	var now = new Date(),
		currHour = now.getHours() > 12 ? now.getHours() - 12 : (now.getHours() < 10 ? "0" + now.getHours() : now.getHours()),
		currMin  = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes(),
		currSec  = now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds(),
		timestamp = currHour + ':' + currMin + ':' + currSec;
		
	console.log(timestamp + ' ' + msg);
}

It was nothing fancy. Calling log('test') would output 11:26:34 test and that was that, got it covered.

After a while, during a period where I tried to polish my short-circuit evaluation skills, I broke down the function into its more explicit form:

function getDate() {
	var now = newDate(),
		cH, cM, cS;
	
	if(now.getHours() > 12) {
		cH = now.getHours - 12;
	} else {
		if(now.getHours() < 10) {
			cH = "0" + now.getHours();
		} else {
			cH = now.getHours();
		}
	}
	
	if(now.getMinutes() < 10) {
		cM = "0" + now.getMinutes();
	} else {
		cM = now.getMinutes();
	}
	
	if(now.getSeconds() < 10) {
		cS = "0" + now.getSeconds();
	} else {
		cS = now.getSeconds();
	}
	
	return cH + ':' + cM + ':' + cS;
}

In case you are wondering how did I end up with that, the answer is rather easy. The following code using short-circuit evaluation:

a = (b > c) ? d : e;

is the equivalent of:

if(b > c) {
    a = d;
} else {
    a = e;
}

The rest is rather easy after understanding that.

Anyway, as some of you will probably also do, I've noticed that the 'lower than 10' part kept repeating so I thought: is there a shorter way to write this function?

Of course there is! After a little fiddling, I've ended up with this:

function nowDate() {
	var now = new Date();
	return ((now.getHours() > 12) ? now.getHours() - 12 : zero(now.getHours())) + ':' + zero(now.getMinutes()) + ':' + zero(now.getSeconds());
}

function zero(nr) {
	return (nr < 10) ? "0" + nr : nr;
}

Much simpler and shorter, but it did use two separate functions and that's not something that I particularly like, I prefer to keep all my eggs in just one basket. And what's a neat way to do this with Javascript? Using an object, of course:

now = {
	z    : function(nr) {
		return (nr < 10) ? "0" + nr : nr;
	},
	get : function() {
		var n = new Date();
		return ((n.getHours() > 12) ? n.getHours() - 12 : this.z(n.getHours())) + ':' + this.z(n.getMinutes()) + ':' + this.z(n.getSeconds());
	}
}

Calling now.get() will output a proper timestamp. And since I wanted a logging function, let's add the following method too:

now = {
	z    : function(nr) {
		return (nr < 10) ? "0" + nr : nr;
	},
	get : function() {
		var n = new Date();
		return ((n.getHours() > 12) ? n.getHours() - 12 : this.z(n.getHours())) + ':' + this.z(n.getMinutes()) + ':' + this.z(n.getSeconds());
	},
        log : function(msg) {
            console.log(this.get() + ' ' + msg)
        }
}

Nice and simple! Now every time I need a timestamped logger, I just call now.log('my message');
And that's that!