假设, 我有日期范围:
var start = moment('2017-08-21').startOf('isoweek').startOf('day');
var end = moment('2017-08-21').startOf('isoweek').add('days', 4).endOf('day');
我有另一个阵列:
var array = [
{x:Mon Aug 21 2017 08:00:00 GMT+0200 (Central European Daylight Time),y:3},
{x:Mon Aug 21 2017 08:05:00 GMT+0200 (Central European Daylight Time),y:6},
...
{x:Mon Aug 22 2017 07:05:00 GMT+0200 (Central European Daylight Time),y:4},
{x:Mon Aug 22 2017 08:05:00 GMT+0200 (Central European Daylight Time),y:3},
{x:Mon Aug 22 2017 15:05:00 GMT+0200 (Central European Daylight Time),y:9},
{x:Mon Aug 22 2017 22:05:00 GMT+0200 (Central European Daylight Time),y:1},
...
{x:Mon Aug 23 2017 06:05:00 GMT+0200 (Central European Daylight Time),y:5},
{x:Mon Aug 23 2017 08:00:00 GMT+0200 (Central European Daylight Time),y:3},
{x:Mon Aug 23 2017 23:05:00 GMT+0200 (Central European Daylight Time),y:3},
...
{x:Mon Aug 25 2017 07:05:00 GMT+0200 (Central European Daylight Time),y:4},
{x:Mon Aug 25 2017 12:05:00 GMT+0200 (Central European Daylight Time),y:3},
{x:Mon Aug 25 2017 15:05:00 GMT+0200 (Central European Daylight Time),y:8},
{x:Mon Aug 25 2017 19:05:00 GMT+0200 (Central European Daylight Time),y:7},
{x:Mon Aug 25 2017 23:05:00 GMT+0200 (Central European Daylight Time),y:3},
...
{x:Mon Aug 27 2017 06:05:00 GMT+0200 (Central European Daylight Time),y:5},
{x:Mon Aug 27 2017 08:00:00 GMT+0200 (Central European Daylight Time),y:3},
{x:Mon Aug 27 2017 23:05:00 GMT+0200 (Central European Daylight Time),y:3},
...
];
如果时间不在以下范围内,我需要做的是更改y = 0的值:
'08:05:00'到22:05:00
表示我的输出必须如下所示:
var array = [
{x:Mon Aug 21 2017 08:00:00 GMT+0200 (Central European Daylight Time),y:0},
{x:Mon Aug 21 2017 08:05:00 GMT+0200 (Central European Daylight Time),y:6},
...
{x:Mon Aug 22 2017 07:05:00 GMT+0200 (Central European Daylight Time),y:0},
{x:Mon Aug 22 2017 08:05:00 GMT+0200 (Central European Daylight Time),y:3},
{x:Mon Aug 22 2017 15:05:00 GMT+0200 (Central European Daylight Time),y:9},
{x:Mon Aug 22 2017 22:05:00 GMT+0200 (Central European Daylight Time),y:1},
...
{x:Mon Aug 23 2017 06:05:00 GMT+0200 (Central European Daylight Time),y:0},
{x:Mon Aug 23 2017 08:00:00 GMT+0200 (Central European Daylight Time),y:0},
{x:Mon Aug 23 2017 23:05:00 GMT+0200 (Central European Daylight Time),y:0},
...
{x:Mon Aug 25 2017 07:05:00 GMT+0200 (Central European Daylight Time),y:0},
{x:Mon Aug 25 2017 12:05:00 GMT+0200 (Central European Daylight Time),y:3},
{x:Mon Aug 25 2017 15:05:00 GMT+0200 (Central European Daylight Time),y:8},
{x:Mon Aug 25 2017 19:05:00 GMT+0200 (Central European Daylight Time),y:7},
{x:Mon Aug 25 2017 23:05:00 GMT+0200 (Central European Daylight Time),y:0},
...
{x:Mon Aug 27 2017 06:05:00 GMT+0200 (Central European Daylight Time),y:0},
{x:Mon Aug 27 2017 08:00:00 GMT+0200 (Central European Daylight Time),y:0},
{x:Mon Aug 27 2017 23:05:00 GMT+0200 (Central European Daylight Time),y:0},
...
];
到目前为止我一直在尝试
$.each(arr, function (key, value) {
var date = moment(value.x);
//if (date < start || date > end])) {
//@TODO where i need to check time interval also
filterArray.push({
x: date,
y: 0
})
} else {
filterArray.push({
x: date,
y: value.y
})
}
});
我试着查看日期范围的时间间隔,但没有运气。任何人都可以向我提出建议或帮助吗?
答案 0 :(得分:0)
我假设您的x
属性是一个JavaScript Date对象,而var date = moment(value.x);
正在调用moment(Date);
。
您可以使用isBetween
:
检查片刻是否在两个其他时刻之间,可选择查看单位比例(分钟,小时,天等)。这场比赛是独家的。
在您的情况下,您可以将'[]'
作为第四个参数传递给isBetween
。
您只需使用date.isBetween(start, end, null, '[]')
来检查元素是否在start
和end
之间。
然后,您可以使用isBetween
检查您的元素时间是否在同一天的08:05:00
和22:05:00
构建3时刻对象之间(今天在我的示例中,因为已省略单位默认为0或当前日期,月份和年份。,如moment({unit: value, ...});
的文档中所述。所以你将拥有:
today805
代表08:05:00
today2205
代表22:05:00
time
这是当天的时刻对象,其x
属性的时间($.each
循环的当前元素的时间)因此,您可以使用time.isBetween(today805, today2205, null, '[]')
检查元素的时间是否介于08:05:00
和22:05:00
之间。这将只比较&#34;&#34; 时间,因为三个时刻代表构造的同一天。
您可以使用时刻的getter(hours()
和minutes()
)以及本机Date getter来获取x
元素的array
属性的小时和分钟(getHours()
和getMinutes()
)
这是一份工作样本:
var start = moment('2017-08-21').startOf('isoweek').startOf('day');
var end = moment('2017-08-21').startOf('isoweek').add(4, 'days').endOf('day');
var array = [
{x: new Date(2017, 7, 21, 8, 0), y:3},
{x: new Date(2017, 7, 21, 8, 5), y:6},
//...
{x: new Date(2017, 7, 22, 7, 5), y:4},
{x: new Date(2017, 7, 22, 8, 5), y:3},
{x: new Date(2017, 7, 22, 15, 5),y:9},
{x: new Date(2017, 7, 22, 22, 5),y:1},
//...
//...
//...
{x: new Date(2017, 7, 27, 6, 5), y:5},
{x: new Date(2017, 7, 27, 8, 0), y:3},
{x: new Date(2017, 7, 27, 23, 5),y:3},
//...
];
var filterArray = [];
// Today at 08:05:00
var today805 = moment({h: 8, m: 5});
// Today at 22:05:00
var today2205 = moment({h: 22, m: 5});
$.each(array, function (key, value) {
var date = moment(value.x);
// Create a moment object for the current day with hours and minutes of value.x
var time = moment({h: date.hours(), m: date.minutes()});
var yVal;
if( date.isBetween(start, end, null, '[]') &&
time.isBetween(today805, today2205, null, '[]') ) {
yVal = 0;
} else {
yVal = value.y;
}
filterArray.push({
x: date.format(),
y: yVal
});
});
console.log(filterArray);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
&#13;