-
-
Save connected/b63c8bbe5c48863bc5958cd652553e48 to your computer and use it in GitHub Desktop.
Calculate start/end dates for common ranges in Javascript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment