0

I have a field called Last Modified At with value like '2016/04/12 20:24:18'. It is not an ISO Date but a normal String value stored via a java process in MongoDb. I am trying to write a shell script to calculate the difference between '2016/04/12 20:24:18' and say '2016/04/12 16:24:18'. The difference could be either in days or hours or mins or secs. I tried couple of things including converting to ISO dates but it doesnt work out. Is there an easy way to find out like Oracle.

Any help would be appreciated?

Thanks, Ram

Ram N
  • 3
  • 1
  • 3
  • Can you use the [edit] link on your question to include the attempts made on converting to ISO dates? – chridam Apr 13 '16 at 07:19

1 Answers1

0

I'm not exactly sure how you plan on running the shell script, but it is possible in the mongo shell to parse dates (using Javascript) and calculate time between two dates as you have asked. Assume we have a document in the things database as follows:

{
    "_id" : ObjectId("570f1e528163383227ace761"),
    "lastModifiedAt" : "2016/04/12 20:24:18"
}

We can run the following script to get the difference between the lastModifiedDate of a document and a hard-coded date, such as 2016/04/12 16:24:18:

db.things.find({}).forEach(function(thing) {
    var date1 = new Date(thing.lastModifiedAt);
    var date2 = new Date('2016/04/12 16:24:18');
    var dateDiff = date1.getTime() - date2.getTime();
    printjson({_id:thing._id,lastModifiedAt:thing.lastModifiedAt,dateDiff:dateDiff});
});

This results in:

{
    "_id" : ObjectId("570f1e528163383227ace761"),
    "lastModifiedAt" : "2016/04/12 20:24:18",
    "dateDiff" : 14400000
}

where dateDiff is milliseconds and 14400000 milliseconds = 4 hours.

If you provide more information on how you plan on making this call, and where the second date is coming from I would be happy to expand upon this answer.

markwatsonatx
  • 3,391
  • 2
  • 21
  • 19