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:;  //prints ISODate(\"yyyy-mm-ddThh:mm:ss.xxxZ")

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

sessionTime.add(70, "minutes");;  // 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?


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


Relevant documentation:

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; so that I can try for that value?



Kinvey Support

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.;  //prints: "ISODate(\"2016-05-13T00:00:00.000Z\")"

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

sessionTime.add(70, "minutes");;  //prints: "null"

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


The only way I found to get around this was to do string operations to get rid of ISODate("") as follows:;
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?



1 person likes this

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

Login or Signup to post a comment