Skip to content

Instantly share code, notes, and snippets.

@connected
Forked from jllodra/date_ranges.js
Created May 30, 2018 13:52
Show Gist options
  • Select an option

  • Save connected/b63c8bbe5c48863bc5958cd652553e48 to your computer and use it in GitHub Desktop.

Select an option

Save connected/b63c8bbe5c48863bc5958cd652553e48 to your computer and use it in GitHub Desktop.

Revisions

  1. @jllodra jllodra renamed this gist Jun 27, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @jllodra jllodra renamed this gist Jun 27, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @jllodra jllodra created this gist Jun 27, 2013.
    44 changes: 44 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    // Calculate and set ranges: today, yesterday, this week, past week, this month, past month, this year
    switch(newValue) {
    case 'today':
    initial_date.setValue(new Date());
    end_date.setValue(new Date());
    break;
    case 'yesterday':
    var d = new Date();
    d.setDate(d.getDate() - 1);
    initial_date.setValue(d);
    end_date.setValue(d);
    break;
    case 'thisweek':
    var d = new Date();
    var first = d.getDate() - d.getDay() + 1;
    var last = first + 6;
    initial_date.setValue(new Date(d.setDate(first)));
    end_date.setValue(new Date(d.setDate(last)));
    break;
    case 'pastweek':
    var d = new Date();
    var first = d.getDate() - d.getDay() - 7 + 1;
    var last = first + 6;
    initial_date.setValue(new Date(d.setDate(first)));
    end_date.setValue(new Date(d.setDate(last)));
    break;
    case 'thismonth':
    var date = new Date(), y = date.getFullYear(), m = date.getMonth();
    initial_date.setValue(new Date(y, m, 1));
    end_date.setValue(new Date(y, m + 1, 0));
    break;
    case 'pastmonth':
    var date = new Date(), y = date.getFullYear(), m = date.getMonth() - 1;
    initial_date.setValue(new Date(y, m, 1));
    end_date.setValue(new Date(y, m + 1, 0));
    break;
    case 'thisyear':
    var date = new Date(), y = date.getFullYear();
    initial_date.setValue(new Date(y, 0, 1));
    end_date.setValue(new Date(y, 11, 31));
    break;
    default:
    break;
    }