function addtoday(){
alert("Today");
}
function addyesterday(){
alert("Yesterday");
}
function lastweek1(){
alert("lastweek");
}
function last30days1(){
alert("last30days");
}
function presentmonth1(){
alert("this month");
}
function lastmonth1(){
alert("lastmonth");
}
function randomfunction(){
alert("randommmmmm");
}
$(function() {
var start = moment().subtract(29, 'days');
var end = moment();
function cb(start, end) {
$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
}
$('#reportrange').daterangepicker({
startDate: start,
endDate: end,
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
}
}, cb);
cb(start, end);
});
$('#reportrange').on('apply.daterangepicker', function (ev, picker) {
if (picker.chosenLabel == "Today") {
addtoday();
}
if (picker.chosenLabel == "Yesterday") {
addyesterday();
}
if (picker.chosenLabel == "Last 7 Days") {
lastweek1();
}
if (picker.chosenLabel == "Last 30 Days") {
last30days1();
}
if (picker.chosenLabel == "This Month") {
presentmonth1();
}
if (picker.chosenLabel == "Last Month") {
lastmonth1();
}
else if(setDate == "10/02/2018" && setDate == "10/20/2018") {
randomfunction();
}
});
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
<div id="reportrange" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; width: 100%">
<i class="fa fa-calendar"></i>
<span></span> <i class="fa fa-caret-down"></i>
</div>
enter image description here我可以在单击标签时执行功能。以同样的方式,我应该可以在单击日历上的随机日期时执行功能
$(function () {
var start = moment().subtract(6, 'days');
var end = moment();
function cb(start, end) {
$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
}
$('#reportrange').daterangepicker({
startDate: start,
endDate: end,
minDate: moment().subtract(365, 'days'),
maxDate: moment(),
ranges: {
'Today': [moment(), moment(),],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days'),],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
},
},
cb)
cb(start, end);
});
$('#reportrange').on('apply.daterangepicker', function (ev, picker) {
if (picker.chosenLabel == "Today") {
addtoday();
}
else if (picker.chosenLabel == "Yesterday") {
addyesterday();
}
else if (picker.chosenLabel == "Last 7 Days") {
lastweek1();
}
else if (picker.chosenLabel == "Last 30 Days") {
last30days1();
}
else if (picker.chosenLabel == "This Month") {
presentmonth1();
}
else if (picker.chosenLabel == "Last Month") {
lastmonth1();
}
else if(picker.startDate == "10/02/2018" && picker.endDate == "10/20/2018"){
randomfunction();
}
});
我能够执行所有功能,但是我无法在代码中执行randomfunction,我可以将这些日期存储在变量中以便动态地对其进行更改
答案 0 :(得分:0)
您没有将比较日期转换为Epoch格式。
编辑:还将您范围内的endDate设置为当天的结束时间,例如23:59。我已经对其进行了修改,以便在进行比较时将其设置为当天的开始时间(00:00)。那可能不是您想要的,但是很容易修改。
function addtoday(){
alert("Today");
}
function addyesterday(){
alert("Yesterday");
}
function lastweek1(){
alert("lastweek");
}
function last30days1(){
alert("last30days");
}
function presentmonth1(){
alert("this month");
}
function lastmonth1(){
alert("lastmonth");
}
function randomfunction(){
alert("randommmmmm");
}
$(function() {
var start = moment().subtract(29, 'days');
var end = moment();
function cb(start, end) {
$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
}
$('#reportrange').daterangepicker({
startDate: start,
endDate: end,
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
}
}, cb);
cb(start, end);
});
$('#reportrange').on('apply.daterangepicker', function (ev, picker) {
if (picker.chosenLabel == "Today") {
addtoday();
}
if (picker.chosenLabel == "Yesterday") {
addyesterday();
}
if (picker.chosenLabel == "Last 7 Days") {
lastweek1();
}
if (picker.chosenLabel == "Last 30 Days") {
last30days1();
}
if (picker.chosenLabel == "This Month") {
presentmonth1();
}
if (picker.chosenLabel == "Last Month") {
lastmonth1();
}
if(picker.startDate == toEpochDate("10/02/2018") && toEpochDate(picker.endDate) == toEpochDate("10/20/2018")) {
randomfunction();
}
});
function toEpochDate(date){
var d = new Date(date);
d.setHours(0,0,0,0);
return d.getTime();
}
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
<div id="reportrange" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; width: 100%">
<i class="fa fa-calendar"></i>
<span></span> <i class="fa fa-caret-down"></i>
</div>