Skip to content

Dates persisted in Mongo inconsistently #603

timhaines opened this Issue Jan 11, 2013 · 10 comments

4 participants


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.

Meteor Development Group member
n1mmy commented Jan 11, 2013

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 Jan 11, 2013

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 ?


I just found this discussion:!newtopic/meteor-talk/meteor-talk/oUy4odf8Mgk

And now I am going to check out moment.

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


The javascript:

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

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

And the html:

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

Thank you !


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.


interesting, thank you very much !


I have another questiion,
how do I sort the post by date when querying with mongo?
I tryed this:
But that did not sort anything ?


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.