Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
$('#today_nav').on("click", function(){
$('#thisweekend_nav').on("click", function(){
$('#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);
function dateSearch(from_date,to_date){
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