我有一个数组propertyDetails
,在这个数组中,我有两个字段,分别为activationDate
和deActivationDate
。我想从activationDate
中提取deActivationDate
和propertyDetails
并计算日期差,然后将其推入propertyDetails
数组中。如何实现呢?
我的JS代码
var doc =
{
"name" : "property",
"propertyDetails" : [
{
"activationDate" : "2018-05-06 20:01:43",
"deActivationDate" : "2018-05-07 14:18:52"
},
{
"activationDate" : "2017-12-15 20:22:18",
"deActivationDate" : null
}
]
}
console.log(doc.propertyDetails[0].pedagogyID);
//date difference calculation code
var date1 = new Date('2013/11/04 00:00:00');
var date2 = new Date('2013/11/05 10:10:10'); //less than 1
var start = Math.floor(date1.getTime() / (3600 * 24 * 1000)); //days as integer from..
var end = Math.floor(date2.getTime() / (3600 * 24 * 1000)); //days as integer from..
var daysDiff = end - start; // exact dates
注意:deActivationDate
为null表示采用当前日期和时间进行计算
答案 0 :(得分:1)
我会使用类似moment.js之类的库。片刻可以让您执行以下操作:
...
const start = moment(date1.getTime())
const end = moment(date2.getTime())
const dateDiff = start.diff(end, 'days')
....
有关矩差here的特定文档。
答案 1 :(得分:1)
您应该遍历propertyDetails数组,并将差异添加到各个对象中。
var doc =
{
"name" : "property name",
"propertyDetails" : [
{
"activationDate" : "2018-05-06 20:01:43",
"deActivationDate" : "2018-05-05 14:18:52"
},
{
"activationDate" : "2017-12-15 20:22:18",
"deActivationDate" : null
},
{
"activationDate" : "2017-12-15 20:21:11",
"deActivationDate" : null
}
]
}
// Loop though all the topics
for (var i = 0; i < doc.propertyDetails.length; i++) {
// If they have a deactivation date, calculate the diff and put into an attribute named "dateDifference"
if (doc.propertyDetails[i].deActivationDate == null) {
doc.propertyDetails[i].deActivationDate = new Date()
}
doc.propertyDetails[i].dateDifference = getDaysDiff(new Date(doc.propertyDetails[i].activationDate), new Date(doc.propertyDetails[i].deActivationDate))
}
// Function to simplify and avoid code repetition.
function getDaysDiff(begin, end) {
var beginDays = Math.floor(begin.getTime() / (3600 * 24 * 1000))
var endDays = Math.floor(end.getTime() / (3600 * 24 * 1000))
return endDays - beginDays
}
答案 2 :(得分:0)
在我看来,您应该只需要像这样遍历对象即可
var counter = 0;
for (details in doc.TopicDetails) {
doc.TopicDetails[counter].differenceInDays = daysDiff;
counter += 1;
}
答案 3 :(得分:0)
var doc =
{
"name" : "name",
"Key" : [
{
"activationDate" : "2018-05-06 20:01:43",
"deActivationDate" : "2018-05-05 14:18:52"
},
{
"activationDate" : "2017-12-15 20:22:18",
"deActivationDate" : null
},
{
"activationDate" : "2017-12-15 20:21:11",
"deActivationDate" : null
}
]
}
//这是更新现有阵列的方法。
doc.Res= doc.Key.map(topic => {
if(!topic.deActivationDate){ topic.deActivationDate = new Date() }
//date difference calculation code
var date1 = new Date(topic.activationDate);
var date2 = new Date(topic.deActivationDate); //less than 1
var start = Math.floor(date1.getTime() / (3600 * 24 * 1000)); //days as integer from..
var end = Math.floor(date2.getTime() / (3600 * 24 * 1000)); //days as integer from..
var daysDiff = end - start; // exact dates
return Object.assign({dateDifference: daysDiff }, topic);
});
console.log(doc.Res);
答案 4 :(得分:0)
您可以使用数组https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach的forEach运算符
var doc =
{
"name" : "name",
"Key" : [
{
"activationDate" : "2018-03-06 20:01:43",
"deActivationDate" : "2018-05-05 14:18:52"
},
{
"activationDate" : "2017-12-15 20:22:18",
"deActivationDate" : null
} ]
}
doc.Key.forEach(function(item){
var date1 = new Date(item.activationDate);
var date2 = !item.deActivationDate?new Date():new Date(item.deActivationDate); //less than 1
var start = Math.floor(date1.getTime() / (3600 * 24 * 1000)); //days as integer from..
var end = Math.floor(date2.getTime() / (3600 * 24 * 1000)); //days as integer from..
item.daysDiff=end - start;
})
console.log(doc);
答案 5 :(得分:0)
const doc = {
"name": "property",
"arrayname": [{
"activationDate": "2018-05-06 20:01:43",
"deActivationDate": "2018-05-07 14:18:52"
},
{
"activationDate": "2017-12-15 20:22:18",
"deActivationDate": null
}
]
}
doc.arrayname.forEach(entry => {
const {
deActivationDate,
activationDate
} = entry;
const activition = Math.floor(new Date(activationDate).getTime() / (3600 * 24 * 1000));
const deActivition = Math.floor(new Date(deActivationDate).getTime() / (3600 * 24 * 1000));
entry.dateDifference = deActivition - activition;
});
console.log(doc);
遵循这些原则即可。修改代码段,它应该对您有用。