1

I have one array propertyDetails and in this array I have two fields called activationDate & deActivationDate. I want to take activationDate & deActivationDate from propertyDetails and calculate the date difference and push it into the propertyDetails array. How can achieve this?

my JS code

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

Note: deActivationDate null means take current date & time and calculate

1
  • Hint: instead of three lines with start, end and daysDiff you could do Math.floor((date2 - date1) / (3600 * 24 * 1000)) to get the number of whole days between dates. Commented Dec 14, 2018 at 16:08

6 Answers 6

1

You should loop through your propertyDetails array and append the difference in the respective object.

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry. The dates are as string, we need to create a Date object before calling the function. Will update the answer.
Check again. Now we use the if condition to check if the deActivationDate exists, if not, we set it as current date. If you don't want to set, you can use a variable to instead
If this answer is the solution for your needs and/or you are using this code, please set this as accepted answer.
My friend, 2017 was last year, my code returns 364 and I believe it's correct. Current date will be 2018-12-14
1

I'd use a library like moment.js or similar. Moment would let you do things like:

...
const start = moment(date1.getTime())
const end = moment(date2.getTime())

const dateDiff = start.diff(end, 'days')
....

Specific docs on Moment Difference here.

2 Comments

Usually it's better to avoid putting a lib answer if the OP did not use it, even though I agree that moment would be nice, that should be a comment.
Including an entire library to solve a simple use-case is a really bad advice. It's bad from every possible perspective.
0

It seems to me you should just be able to iterate through the object like

var counter = 0;
 for (details in doc.TopicDetails) {
    doc.TopicDetails[counter].differenceInDays = daysDiff;
    counter += 1;
 }

3 Comments

Please, put stuff like that as comments.
@RenanSouza sorry, still learning the ropes. Just went ahead and took it out.
No problem, really @dillon.harless, just keep in mind that sometimes its better to use the comments instead of putting lots of unncessary answers.
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);

Something along those lines will do. Modify the snippet and it should work for you.

Comments

0

You can use forEach operator of Array https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Array/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);

3 Comments

you ca invert date1, date2 for diff to obtain positive or negative difference
deActivationDate null means ?
I'll suggest to remove 'if (item.deActivationDate==null) return item.dayDiff=0;' and modify var date2 assignement in: 'var date2 = !item.deActivationDate?new Date():new Date(item.deActivationDate);'
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
            }
        ]
    }

// this is how you can update your existing array.

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);

3 Comments

deActivationDate null means ?
Yes, updated the code. thank for correcting. @Prasanna
deActivationDate null means we have to take current take then culate not for 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.