Page 1 of 2

parsing date string gives NaN

Posted: Wed Apr 08, 2015 1:31 am
by Ben Walker

I am trying to create server code to delete an item in a DB, if it was created more than 10 minutes ago.

It's not working, and I've identified that the problem is the var createdAt below is NaN. This throws off all the math below, with the result that

if (minutesOpen 10)

cannot be evaluated.

Here is all the code:

function deleteOpenMarkers() {
var openSpots = Collection.query(dbId, "openSpots");

console.log(openSpots.length);

for(var i = 0; i < openSpots.length; i++) {

Code: Select all

 var createdAt = openSpots[i]._createdAt; 
 var ms1 = Date.parse(createdAt); 
 var ms2 = new Date().getTime(); 
 var minutesOpen = Math.round((ms2 - ms1)/1000/60 + 420); 
 var spotId = openSpots[i]._id; 

 if (minutesOpen  10) { 
   Collection.deleteObject(dbId, "openSpots", spotId); 
 } else { 
   console.log("spot still open"); 
 } 

}
}

deleteOpenMarkers();

Any help much appreciated!


parsing date string gives NaN

Posted: Wed Apr 08, 2015 4:10 am
by Bruce Stuart

Hi Ben,

Sorry if I'm asking what's already obvious ... What is the value of 'i' at the point of failure, what is the length of the result set, and what is the value of openStops ._createdAt v. What's on the db for that key ? Also in the open spots db ... What is the value of the _id for that open spot ... And does it match what is expected?

Lots of console logging ...


parsing date string gives NaN

Posted: Wed Apr 08, 2015 6:25 pm
by Ben Walker

In the test I was doing, I can be either 0 or 1. There are only two items in the DB - failure occurs at both points in the for loop.

openSpots._createdAt correctly pulls a datestring from the DB, it looks like this:

{ '$date': '2015-04-08T03:04:44.317Z' }

Perhaps that is the problem - does this expression have any meaning?

Date.parse({ '$date': '2015-04-08T03:04:44.317Z' })

I have got the Date.parse method to work well on just a date string part... but not sure why it's in {}.

Thanks!


parsing date string gives NaN

Posted: Wed Apr 08, 2015 6:30 pm
by Ben Walker

The thinking is that Date.parse will return the number of ms since epoch..

But it's not returning a number, so when I do math on it, I get a NaN..


parsing date string gives NaN

Posted: Thu Apr 09, 2015 1:57 pm
by Serhii Kulibaba

Hello Ben,

Please follow this tutorial: https://devcenter.appery.io/documenta...


parsing date string gives NaN

Posted: Thu Apr 09, 2015 4:36 pm
by Ben Walker

Hi - that tutorial seems to deal with accessing a DB from the app platform, but I'm trying to access it using server code.

The question I have, is how do I query the DB for a "normal" datestring, in the format 2015-04-09 16:12:17.578?

Using server code:
var openSpots = Collection.query(dbId, "openSpots");
var createdAt = openSpots._createdAt;

The format of _createdAt is { '$date': '2015-04-09T16:12:17.578Z' }

I can access the same _createdAt var from the app platform using the list_service:

var createdAt = data._createdAt;

and the format is 2015-04-09 16:12:17.578


parsing date string gives NaN

Posted: Thu Apr 09, 2015 6:33 pm
by Bruce Stuart

Here's a snippet of code that I use to accomplish date queries --- from a date that comes in....of date type....

function dbDate(dinput) {
var sreturn = dinput.toISOString().substring(0, 23); // need 23 characters to do a good date & TIME search.....
sreturn = sreturn.replace("T", " "); // remove the t that comes back for searching
return sreturn;
}

Also - I have a host of date functions that run on the server side that anyone can use.... here's the link.

http://the-software-studio.com/javasc...

Best,

Bruce


parsing date string gives NaN

Posted: Thu Apr 09, 2015 10:10 pm
by Ben Walker

Thanks Bruce - didn't need all of that, but it helped and I now have it working.


parsing date string gives NaN

Posted: Fri Apr 10, 2015 1:14 am
by Bruce Stuart

Ben, perfect ! Good to hear. Better a bucket than an eye dropper perhaps !


parsing date string gives NaN

Posted: Tue Aug 04, 2015 5:26 pm
by RK

Hi Ben, I am facing the same issue. Can you please guide as to how were you able to get over this problem?