I am in the process of stripping down some code I've written for a Time Frame selector function that filters search results based on a selected Time Frame (Last Month, Last Quarter & Last Year) by hiding all irrelevant results based on when they were created. The function is called by a GET variable submitted on form submit ?time_frame_query=Last+Month
Now the code as is works, which is a start. However I feel it could be greatly improved, and unfortunately I have exhausted all options trying to do this myself. The problem I am having is that by the time I get to writing the functions for the Last Year filter, I am having to write a huge amount of code which I feel isn't entirely necessary. I have toyed with the idea of putting all the month variables into an array then putting together a specific "Last Month" "Last Quarter" "Last Year" function that will do the work for me. But again given my current knowledge of JS/jQuery, I simply do not know how to go about doing this.
Below is a snippet of code that I feel is relevant to my question and also helps illustrate the problem I am facing. Please let me know if you need to see any more.
EDIT: Thanks to Doug the entire working code can be found here: http://pastie.org/private/wl2qvnyar5xpibu4ot1lig
Any help/advice would be greatly appreciated.
var timezone = "Australia/ACT";
$.getJSON("http://json-time.appspot.com/time.json?tz="+timezone+"&callback=?",
function(data){
var datetime = data.datetime
var date = datetime.split(" ")
var day = date[1]
var month = date[2]
var year = date[3]
var prevYear = year - 1
var jan = 'Jan'
//etc etc etc
var dec = 'Dec'
var timeFrame = $.getUrlVar('time_frame_query');
switch(timeFrame){
//****************************
// Begin Last Quarter function
//****************************
case 'Last+Quarter':
switch(month){
case 'Jan':
$('#searchResults div.'+oct+prevYear+',#searchResults div.'+nov+prevYear+',#searchResults div.'+dec+prevYear+',#searchResults div.'+jan+year).show();
break;
case 'Feb':
$('#searchResults div.'+nov+prevYear+',#searchResults div.'+dec+prevYear+',#searchResults div.'+jan+year+',#searchResults div.'+feb+year).show();
break;
case 'Mar':
$('#searchResults div.'+dec+prevYear+',#searchResults div.'+jan+year+',#searchResults div.'+feb+year+',#searchResults div.'+mar+year).show();
break;
case 'Apr':
$('#searchResults div.'+jan+year+',#searchResults div.'+feb+year+',#searchResults div.'+mar+year+',#searchResults div.'+apr+year).show();
break;
// etc etc etc
}
break;
Appologies for the poorly formatted code above, first submission to this site. WYSIWYG FTL! :(