我有以下脚本显示表格,其中包含每周发生的交易。
我想每月和每天都这样做。我每个月都遇到麻烦,因为一年中的所有月份都不会有相同的间隔。
脚本
$('#test').click(function () {
// Here are the two dates to compare
var date1 = '29-10-2015';
var date2 = '29-12-2015';
var Targetvalue = parseFloat("1000000");
var dealjson = '[{"dealdate":"25-12-2015","cost":200000},{"dealdate":"25-11-2015","cost":200000}]';
// First we split the values to arrays date1[0] is the year, [1] the month and [2] the day
date1 = date1.split('-');
date2 = date2.split('-');
// Now we convert the array to a Date object, which has several helpful methods
date1 = new Date(date1[2], date1[1], date1[0]);
date2 = new Date(date2[2], date2[1], date2[0]);
var deals = JSON.parse(dealjson);
var achieved = 0;
date1_unixtime = parseInt(date1.getTime() / 1000);
date2_unixtime = parseInt(date2.getTime() / 1000);
// This is the calculated difference in seconds
var timeDifference = date2_unixtime - date1_unixtime;
// in Hours
var timeDifferenceInHours = timeDifference / 60 / 60;
// and finaly, in days :)
var timeDifferenceInDays = timeDifferenceInHours / 24;
var timeDifferenceInWeeks = Math.round(timeDifferenceInDays / 7);
// alert(timeDifferenceInDays/7);
TargetPerweek = Targetvalue / timeDifferenceInWeeks;
TargetPerday = Targetvalue / timeDifferenceInDays;
//Math.round(timeDifferenceInWeeks);
TargetPerweek = Math.round(TargetPerweek * 100) / 100;
var str = '<table class="table table-striped table-bordered bootstrap-datatable datatable responsive"> <thead><tr> <th>Week</th> <th>Target</th> <th>Achieved</th> </tr></thead>';
var i = 0;
while (date1 <= date2) {
var next_week = new Date(date1);
next_week.setDate(date1.getDate() + 7);
achieved = 0;
deals.forEach(function (deal) {
var dealDate = deal.dealdate;
dealDate = dealDate.split('-');
dealDate = new Date(dealDate[2], dealDate[1], dealDate[0]);
if (dealDate >= date1 && dealDate <= next_week) {
achieved = achieved + deal.cost;
}
});
i = i + 1;
str = str + "<tr><th>Week" + i + "</th><td>" + TargetPerweek + "</td><td> " + achieved + "</td></tr>";
date1 = next_week;
}
str = str + "</table>";
$('.varianceData').html(str);
});
基本上,如果你观察到这部分代码
var str = '<table class="table table-striped table-bordered bootstrap-datatable datatable responsive"> <thead><tr> <th>Week</th> <th>Target</th> <th>Achieved</th> </tr></thead>';
var i = 0;
while (date1 <= date2) {
var next_week = new Date(date1);
next_week.setDate(date1.getDate() + 7);
achieved = 0;
deals.forEach(function (deal) {
var dealDate = deal.dealdate;
dealDate = dealDate.split('-');
dealDate = new Date(dealDate[2], dealDate[1], dealDate[0]);
if (dealDate >= date1 && dealDate <= next_week) {
achieved = achieved + deal.cost;
}
});
i = i + 1;
str = str + "<tr><th>Week" + i + "</th><td>" + TargetPerweek + "</td><td> " + achieved + "</td></tr>";
date1 = next_week;
}
您将了解我如何构建周向表,因此我只是将循环递增7次。
我想显示另一个带有月亮和日期明确的表