【发布时间】:2016-10-19 02:57:30
【问题描述】:
我已经定义了一套规则。
var rules = [
{
time : "12:00:00 pm",
time1 : "01:00:00 pm",
value:0
},
{
time : "01:00:00 pm",
time1 : "02:00:00 pm",
value:0
},
{
time : "02:00:00 pm",
time1 : "03:00:00 pm",
value:0
},
{
time : "03:00:00 pm",
time1 : "04:00:00 pm",
value:0
}, {
time : "04:00:00 pm",
time1 : "05:00:00 pm",
value:0
}
]
module.exports = rules;
现在我有一个数据数组,其中包含我想将数据与这些规则进行比较的时间,并相应地增加规则数组中的状态值。 我正在使用 moment.Js 进行比较,但它的 isAfter() 和 isBefore 函数并不适合我。
_.each(data, function(data){
// this comes from database where data.start_time consists of time
console.log(data.start_time); // Mon Oct 03 2016 12:40:36 GMT+0530 (India Standard Time)
var d = moment(data.start_time).format("hh:mm:ss a");
console.log(d); //12:40:36 pm
_.each(rules, function(rule){
console.log(rule.time); //12:00:00 pm
if(moment(d).isAfter(rule.time))
{
// Not able to reach here tried both isAfter
// isBefore everything.
console.log("sasa");
}
})
});
此外,如果有任何更快或更好的方法来进行此类比较,请您在下面留下要点。我也去看看。
【问题讨论】:
标签: javascript node.js momentjs