我试图创建一系列日期,这些日期可以按每日或每周粒度进行解析并遇到障碍。我有一个功能,允许您输入开始月份,开始年份,结束月份,结束年份和粒度,并根据粒度返回日期范围。
我能够以每日粒度成功运行此功能,但在每周粒度运行时,每个新月都会重新启动1.查看以下示例:
2015-11-01 2015-11-08 2015-11-15 2015-11-22 2015-11-29 2015-12-01 2015-12-08 2015-12-15 2015-12-22 2015-12-29 2016-01-01 2016-01-08
我希望它显示为:
2015-11-01 2015-11-08 2015-11-15 2015-11-22 2015-11-29 2015-12-06 2015-12-13 2015-12-20 2015-12-27 2016-01-03
我理解为什么会发生这种情况 - 在每个for循环结束时变量" d"强制设置为1.我尝试在" dates.push之后添加额外的if语句(y +" - " + m +" - " + d)& #34;但我知道这是非常低效的编码。我添加了下面尝试的内容:
if monthday(m,y) – d < 7 {
d = monthday(m,y) –d
if m = 12 {
m = 1
y = y+1
else {
m = m+1
}
}
使用的功能复制如下。
对我如何做到这一点的任何见解都将非常感激!
/// Function to decide how many days in a month
function monthday (month,year) {
var months31 = [1,3,5,7,8,10,12]
var months30 = [4,6,9,11]
var leapyear = [2016,2020,2024,2028,2032] /// if this code is still being used in 2036 I'll eat my hat
if (months31.indexOf(month) >=0){
var result = 31}
else if (months30.indexOf(month) >=0){
var result = 30}
else if (month==2 && leapyear.indexOf(year) >=0){
var result = 29}
else if (month==2 && year != 2016){
var result = 28}
return result
}
////Date Range - calculates # of days/weeks between 2 date ranges
function dateRange (start_month,start_year,end_month,end_year,granularity) {
var dates = [];
var d0 = [start_year,start_month];
var d1 = [end_year,end_month];
switch (granularity) {
case "Daily":
for (var y = d0[0]; y <= d1[0]; y++) {
if ((y == d0[0]) && (d0[0] != d1[0])) { // if year=start_year && year != end year ... start from start_month and loop up to month 12
for (var m = d0[1]; m <= 12; m++) {
for (var d =1;d <= monthday(m,y); d++) {
dates.push(y+"-"+m+"-"+d)
}
}
}
if ((y != d0[0]) && (y!= d1[0])) { // if year != start_year && year != end year .... start from month 1 to month 12 - this would 2015 data in pulling Dec 2014 - April 2016
for (var m = 1; m <= 12; m++) {
for (var d =1;d <= monthday(m,y); d++) {
dates.push(y+"-"+m+"-"+d)
}
}
}
if ((y != d0[0]) && (y == d1[0])) { // if year !=start_year && year = end_year .... start from month 1 up until end_month
for (var m = 1; m <= d1[1]; m++) {
for (var d =1;d <= monthday(m,y); d++) {
dates.push(y+"-"+m+"-"+d)
}
}
}
if ((y == d0[0]) && (y == d1[0])) { /// if year=start_year && year = end_year .... start from start_month to end_month
for (var m = d0[1]; m <= d1[1]; m++) {
for (var d =1;d <= monthday(m,y); d++) {
dates.push(y+"-"+m+"-"+d)
}
}
}
}
break;
case "Weekly":
for (var y = d0[0]; y <= d1[0]; y++) {
if ((y == d0[0]) && (d0[0] != d1[0])) { // if year=start_year && year != end year ... start from start_month and loop up to month 12
for (var m = d0[1]; m <= 12; m++) {
for (var d =1;d <= monthday(m,y); d+=7) {
dates.push(y+"-"+m+"-"+d)
}
}
}
if ((y != d0[0]) && (y!= d1[0])) { // if year != start_year && year != end year .... start from month 1 to month 12 - this would 2015 data in pulling Dec 2014 - April 2016
for (var m = 1; m <= 12; m++) {
for (var d =1;d <= monthday(m,y); d+=7) {
dates.push(y+"-"+m+"-"+d)
}
}
}
if ((y != d0[0]) && (y == d1[0])) { // if year !=start_year && year = end_year .... start from month 1 up until end_month
for (var m = 1; m <= d1[1]; m++) {
for (var d =1;d <= monthday(m,y); d+=7) {
dates.push(y+"-"+m+"-"+d)
}
}
}
if ((y == d0[0]) && (y == d1[0])) { /// if year=start_year && year = end_year .... start from start_month to end_month
for (var m = d0[1]; m <= d1[1]; m++) {
for (var d =1;d <= monthday(m,y); d+=7) {
dates.push(y+"-"+m+"-"+d)
}
}
}
}
break;
}
return dates
}
答案 0 :(得分:0)
您不需要控制月份长度,让Date对象为您执行此操作。
function formatDate(date) {
return date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).substr(-2) + '-' + ('0' + date.getDate()).substr(-2);
}
function dateRange (start_month,start_year,end_month,end_year,granularity) {
var date = new Date(start_year + '-' + start_month + '-01');
var endDate = new Date(end_year + '-' + end_month + '-01');
var arr = [];
while(date < endDate) {
arr.push(formatDate(date));
switch (granularity) {
case 'Daily':
date.setDate(date.getDate() + 1); // date++
break;
case 'Weekly':
date.setDate(date.getDate() + 7); // week++
break;
case 'Monthly':
date.setMonth(date.getMonth() + 1);
break;
// ...
}
}
return arr;
}
这是你要找的东西吗?