Skip to content

Instantly share code, notes, and snippets.

@twoblokeswithapostie
Created March 5, 2014 01:29
Show Gist options
  • Save twoblokeswithapostie/9359477 to your computer and use it in GitHub Desktop.
Save twoblokeswithapostie/9359477 to your computer and use it in GitHub Desktop.
Code using Momentjs for finding, today, this weekend, this months and next month to and from dates
// JavaScript code that finds dates for today, this weekend, this month, next month
// This weekend = next Sat and Sun
// This month = today to end of this month
// Next month = 1st of next month = end of next month
// Useful on Business Catalyst web app search form for searching from-to dates
// Requires jquery and moment.js
$(document).ready(function(){
$('#today_nav').on("click", function(){
dateSearch(moment().format('DD-MMM-YYYY'),moment().format('DD-MMM-YYYY'));
});
$('#thisweekend_nav').on("click", function(){
dateSearch(moment().day(6).format('DD-MMM-YYYY'),moment().day(7).format('DD-MMM-YYYY'));
});
$('#thismonth_nav').on("click", function(){
dateSearch(moment().format('DD-MMM-YYYY'),moment().add('months', 1).date(0).format('DD-MMM-YYYY'));
});
$('#nextmonth_nav').on("click", function(){
var next_month = moment().get('month') + 1;
var now = moment();
var first_next_month = nextMonth(now,next_month).format('DD-MMM-YYYY');
dateSearch(first_next_month,moment().add('months', 2).date(0).format('DD-MMM-YYYY'));
});
$('#featured_nav').on("click", function(){
$('#featured_checkbox').prop('checked', true);
$('#geelong_search_form').submit();
});
function dateSearch(from_date,to_date){
$('#from_date').val(from_date);
$('#to_date').val(to_date);
$('#geelong_search_form').submit();
}
function nextMonth(date, month) {
var input = moment(date);
var output = input.clone().startOf('month').month(month);
return output > input ? output : output.add('years', 1);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment