Start a new topic

How to create a new moment from an ISODate?

I have a collection whose entities have "time" attributes. I work with the dates as NSDates on my iOS client, and Kinvey converts them to ISODate objects, which it stores in the database.


I'd like to write an onPostSave hook that gets the time from the saved entity, adds 75 minutes, and adds the date as a string to a push payload.


I'm attempting to use moment.js to add the time, but I can't seem to create a moment from the saved ISODate.


My code currently looks like this:

  

modules.logger.info(response.body.time);  //prints ISODate(\"yyyy-mm-ddThh:mm:ss.xxxZ")

var sessionTime = modules.moment(response.body.time);
modules.logger.info(sessionTime);  //prints "Invalid time"

sessionTime.add(70, "minutes");
modules.logger.info(sessionTime);  // prints "Invalid time"

 Can someone see what I'm missing? How do I work with moment with the ISODates stored in the Kinvey data store?


That workaround works perfectly. Thanks! I'm using KinveyKit 1.40.7 in my iOS app.

Hi,


The only way I found to get around this was to do string operations to get rid of ISODate("") as follows: 

modules.logger.info(response.body.time);
var sessionTime = response.body.time;
sessionTime = testTime.replace('ISODate("',"");
sessionTime = testTime.replace('")',"");
sessionTime = modules.moment(sessionTime);

 

But, this is a workaround.


Can you tell me the iOS SDK version you are using so that I can try to recreate this issue and see if there's any changes required on Kinvey side?



Regards,

Wani


1 person likes this

I tried adding the toDate() method as you suggested, and now it "null" where before it printed "Invalid date". It seems like Business Logic is not treating ISODate objects the same as a Date objects.


Here is my code with the change you suggested and output from real data.

 

modules.logger.info(response.body.time);  //prints: "ISODate(\"2016-05-13T00:00:00.000Z\")"

var sessionTime = modules.moment(response.body.time);
modules.logger.info(sessionTime.toDate());  //prints: "null"

sessionTime.add(70, "minutes");
modules.logger.info(sessionTime.toDate());  //prints: "null"

 If I remove toDate from lines 3 and 6, they print: "Invalid date".

Hi,


Can you please try following snippet and let me know if it works for you?

  

  var sessionTime = modules.moment(response.body.time);
  sessionTime.add(75, "minutes");
  modules.logger.info(sessionTime.toDate()); 

  


Relevant documentation: http://devcenter.kinvey.com/rest/reference/business-logic/reference.html#moment-module


I was able to test this by replacing response.body.time with new Date().


If this doesn't work, can you send an actual value printed in modules.logger.info(response.body.time); so that I can try for that value?


Regards,

Wani

Kinvey Support

Login or Signup to post a comment