Categories
code

Javascript: Finding the First Monday of the Month

Inspired by a PHP version of this found here, here is a Javascript function that takes the number of the month (0-11) and the year and returns the Date object of the first Monday in that month.  I find it useful for determining weeks in the month.


 // get first Monday of the month, useful for determining week durations

 // @param - integer: month - which month

 // @param - integer: year - which year

  function firstMonday (month, year){

 var d = new Date(year, month, 1, 0, 0, 0, 0)

 var day = 0

// check if first of the month is a Sunday, if so set date to the second

 if (d.getDay() == 0) {

 day = 2

 d = d.setDate(day)

 d = new Date(d)

 }

// check if first of the month is a Monday, if so return the date, otherwise get to the Monday following the first of the month

 else if (d.getDay() != 1) {

 day = 9-(d.getDay())

 d = d.setDate(day)

 d = new Date(d)

 }

 return d  

 }

Please let me know if you find it useful.  Enjoy!

function firstMonday(month, year) { var d = new Date(year, month, 1, 0, 0, 0, 0) var day = 0 if (d.getDay() == 0) { day = 2 d = d.setDate(day) d = new Date(d) } else if (d.getDay() != 1) { day = 9-(d.getDay()) d = d.setDate(day) d = new Date(d) } return d.toString() } firstMonday(10, 2011)
Categories
web development

jQuery, my love …

I have a new fetish (web development fetish, of course) and that would be jQuery.  I had been introduced to it a few weeks back thanks to that most awesome of instructors Hilary Mason (www.hilarymason.com), but now having seen the man himself John Resig present on it at BarCamp Boston 3, I am yet more eager to explore its marvelous functionality.

John Resig’s presentation (to be posted on Youtube before too long, according to kind audience member who thought to record it) was a wonderful overview, going from nuts to bolts in a half an hour.  He explained that jQuery is a Javascript library which, at a minified 15k, provides a way for HTML and Javascript to interact that usually takes server-side scripting to accomplish.  In fact it can act as its own programming language even to the extent of replacing some server-side code. 

Besides enabling cool visual effects such as the toggling of show/hide, accordion drop down menus, and custom animations, it allows manipulation of DOM elements such as your div tags and header tags.  PHP groupies will love the dollar sign syntax, and those who have mastered the CSS #id and .class tags will find learning jQuery an easy transition.

What really perked my ears were two features that I am dying to try out – the AJAX call to load an HTML page and then strip out only those elements that are needed, and the chaining method in which an element is acted upon by multiple events, in a series that looks like this:  div.show().foo().bar() , where show, foo and bar all act on the same element to accomplish their respective functions.

Three books I plan on purchasing in sequence based on Mr. John’s recommendation are Learning jQuery (Packt), jQuery Reference Guide (Packt), and jQuery in Action (Manning) with none other than John Resig writing the foreword.

Interested in learning more about jQuery?  Check it out at jquery.com .  John Resig’s blog is ejohn.org/blog