2

In a cloud function like the following, I am having problems comparing dates:

Parse.Cloud.define(“myCloudFunction”, function(request, response)
  {
    var query = new Parse.Query(“MyClass”);
    query.find
    ({
      success: function(resultList) {
        var today = new Date();

        for (var i = 0; i < resultList.length; ++i) {
          if (resultList[i].get(“dateFieldOne”)>resultList[i].get(“dateFieldTwo”)) {
            // Do something!!
          } else {
            // Do some other thing!!
            if (resultList[i].get(“dateFieldOne”)>today) doOneMoreThing();
          }
        }

        response.success(resultList);
      },
      error: function() {
        response.error("Things have gone wrong.”);
      }
    });
  });

The lines :

if (resultList[i].get(“dateFieldOne”)>resultList[i].get(“dateFieldTwo”))
if (resultList[i].get(“dateFieldOne”)>today) doOneMoreThing();

are not working.

What is the syntax I am supposed to use in this context, to compare the two fields dateFieldOne and dateFieldTwo of type Date?

Timothy Walters
  • 16,866
  • 2
  • 41
  • 49
Michel
  • 10,303
  • 17
  • 82
  • 179

2 Answers2

4

The Moment library is your friend here:

var moment = require('moment');
Parse.Cloud.define("myCloudFunction", function(request, response) {
  var query = new Parse.Query(“MyClass”);
  query.find
  ({
    success: function(resultList) {
      var today = new moment();

    for (var i = 0; i < resultList.length; ++i) {
      var dateFieldOne = moment(resultList[i].get("datefieldOne"));
      var dateFieldTwo = moment(resultList[i].get("dateFieldTwo"));
      if (dateFieldOne.isAfter(dateFieldTwo)) {
        // Do something!!
      } else {
        // Do some other thing!!
        if (dateFieldOne.isAfter(today)) doOneMoreThing();
      }
    }
    // ...etc...

With Moment you can also query for the level of granularity you need, e.g.:

var today = moment();
var tomorrow = moment().add(1, 'days');
// assuming today is 3pm on 2014-07-09
moment("2014-07-09T20:00:00").isBefore(today); // false (8pm is not before 3pm)
moment("2014-07-09T10:00:00").isBefore(today); // true (10am is before 3pm)
moment("2014-07-09T10:00:00").isBefore(today, 'day'); // false (ignoring time, they are equal)
moment("2014-07-09T10:00:00").isBefore(tomorrow, 'day'); // true
moment("2014-07-09T20:00:00").isBefore(tomorrow, 'day'); // true

UPDATE: Note that isBefore() and isAfter() are only available after version 2.0 and Parse.com is using 1.7. If you want to have access to these methods, you can download moment.js and place it in your /cloud directory.

Julian B.
  • 3,605
  • 2
  • 29
  • 38
Timothy Walters
  • 16,866
  • 2
  • 41
  • 49
  • 1
    This sounds interesting, but I still have to figure out how to use this in Parse. Even though I included : var moment = require('moment'); in my code. I does work and I get an error : Error: TypeError: Object Tue Jul 08 2014 15:56:01 GMT+0000 (UTC) has no method 'isAfter' – Michel Jul 08 '14 at 16:01
  • Since I have never used Cloud Modules, I may well be doing something wrong. – Michel Jul 08 '14 at 16:06
  • Is this: var itemDate = moment(resultList[i].get("createdAt")); supposed to work? – Michel Jul 08 '14 at 16:18
  • I have experimented unsuccessfully at this point. Obviously I am missing something basic. – Michel Jul 08 '14 at 16:27
  • Perhaps post an update to your question with your updated code? The `itemDate` line should work and give you a `moment` instance you can call methods like `isAfter()` on. Use lots of `console.log()` lines to see what is happening in code when debugging things like this. – Timothy Walters Jul 08 '14 at 21:54
  • Do I need to download or install something before the "moment module" can work? – Michel Jul 08 '14 at 23:10
  • No, it is one of the built in Cloud Modules described in the documentation. You just need the `require` line. – Timothy Walters Jul 08 '14 at 23:12
  • Thanks for you tips, but ... it still doesn't ... :) .. I made another post: http://stackoverflow.com/questions/24643412/trouble-using-the-moment-module – Michel Jul 09 '14 at 00:03
  • The thing now works after uploading the new version of moment.js. By the way I have placed some console.log() in my code, but I must not be using it the right way because it displays nothing that I can see. – Michel Jul 09 '14 at 03:47
  • Console logs will show up in the Parse.com Dashboard, and also if you use the command line tool `parse log`. – Timothy Walters Jul 09 '14 at 06:56
1

For this code

for (var i = 0; i < resultList.length; ++i) {
          if (resultList[i].get(“dateFieldOne”)>resultList[i].get(“dateFieldTwo”)) {
            // Do something!!
          } else {
            // Do some other thing!!
            if (resultList[i].get(“dateFieldOne”)>today) doOneMoreThing();
          }
        }

First convert it to unix timestamps and then compare them directly.

  for (var i = 0; i < resultList.length; ++i) {
              if (new Date(resultList[i].get(“dateFieldOne”)).getTime()>new Date(resultList[i].get(“dateFieldTwo”)).getTime()) {
                // Do something!!
              } else {
                // Do some other thing!!
                if (new Date(resultList[i].get(“dateFieldOne”)).getTime()>today) doOneMoreThing();
              }
            }
rahil sharma
  • 826
  • 7
  • 16