Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Dates persisted in Mongo inconsistently #603

Closed
timhaines opened this Issue · 10 comments

4 participants

@timhaines

It seems when you update a mongo document from the server new Date() gets stored as an ISODate, but when you update it from the client, new Date() gets stored as a string representation.

I presume the ideal solution is for the client to match the server implementation.

@n1mmy
Owner

Yeah, Date handling is inconsistent current. This is fixed on the ddp-pre1 branch, which handles complex data types like Date and mongo ObjectId.

This branch is getting ready to land, and will hopefully ship in either the next meteor release or the one after.

More info in #61.

@n1mmy n1mmy closed this
@daslicht

So how do I properly save my dates ?
Currently I am just saving my dates (on the server) using the output of date(), which results into something like this:
"date" : ISODate("2013-06-18T16:32:48.506Z")

But If I like to do something useful with it after reading it back I am stuck.

For example I created this Handlebars Helper:

Handlebars.registerHelper('aDate', function(dateObj) {

  var d=new Date(dateObj);
   return  d.toString('yyyy-MM-dd');  

}); 

If i do a console.log() on dateObj i get: "Tue Jun 18 2013 18:32:48 GMT+0200 (CEST)"
and the return value is the same ?!

How do I format stored Dates ?

@daslicht

I just found this discussion:
https://groups.google.com/forum/#!newtopic/meteor-talk/meteor-talk/oUy4odf8Mgk

And now I am going to check out moment.

Thank your very much for your... time ;)

@awatson1978

The javascript:

// add today's date into a record in mongo
var row.date =  moment().format("YYYY-MM-DD");
Schedule.insert(row);

// displays todays date in a template
Template.homePage.today = function(){
        return moment().format("MMM Do YYYY");
};

And the html:

<template name="homePage">
   <span>{{ today }}</span>
</template>
@daslicht

Thank you !

@timhaines

FWIW we're storing the numeric format, and it's worked for us. We took it from what Meteor was doing at the time.

You can grab it like this: +(new Date())

It's not as readable when coming straight from the DB as the format you're using, but I think that's the only downside.

@daslicht

interesting, thank you very much !

@daslicht

I have another questiion,
how do I sort the post by date when querying with mongo?
I tryed this:
http://docs.mongodb.org/manual/reference/operator/orderby/
But that did not sort anything ?

@daslicht

I saved the date now as timestamp and the query looks like this:
return Posts.find({}, { sort: { date: 1 }});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.