Sorcerer's Tower

Entries tagged "JavaScript"

Found 2 entries tagged with "javascript".

Happy Regex Day 2012!

Today was the 1st June, and that means it's Regex Day again!

This annual event was started four years ago by Ben Nadel to celebrate this wonderful (yet often misunderstood) technology, and as usual Ben is running a fun regex competition, with prizes, on his blog.

If that's not enough regex goodness for you, here's a couple of projects you should know about.

First up, there's XRegExp, a library by Steven Levithan which augments JavaScript's native regex functionality with new functionality, and ensures cross-browser compatibility.

For CFML developers, there's cfRegex a replacement regex implemenation providing more power and functionality than CFML's native functions, whilst being easier to work with.

Whatever your level of regex skill, both of these tools are definitely worth checking out.

JavaScript Leap Year check

I've just needed to fix a calendar that didn't implement leap years, and thus was missing 29th Feb this year.

Unfortunately, Google was bringing up various functions that rely on how browsers handle oddities in the built-in date functions, which isn't a sensible approach.


So, here is how to do it relying on the leap year formula:

function isLeapYear(year)
{return ((year%4 == 0) && (year%100 != 0 || year%400 == 0));}

And to implement that:

function readDaysInMonth(month,year)
{
	if (month == 1 && isLeapYear(year) == true) return 29;
	else return [31,28,31,30,31,30,31,31,30,31,30,31][month];
}

(Remember, in JS months are 0-indexed, hence 1 = Feb)