If date = today, show todays events

Hi everyone - I am having a heck of a time trying to do something that should be very simple. I have created a form using autoform that collects two values - a title and a date. I now want to display these event on a page - ONLY if the event equals today.

I can get it to work for events before today and after today - but since minimongo for some reason does not support $eq - I cannot get it to work - what am I doing wrong???

Template.eventsView.helpers({
    event: function() {
        var isToday = new Date();
        return Events.find({
            date: isToday
        });
    }
});

I’ve tried not / not equals too but to no luck. Can Someone please help me out?

You just want the day, not the current time as well, which is what you get when you create a new Date.

Personally I would get the Date that is: currentDate - 24hours

And then would ask for anything that is newer then that.

For your reference:
http://momentjs.com/


Use a helper library like http://momentjs.com/

Rolling your own data functions is only asking for trouble :slight_smile:

1 Like

Not exactly, I want to view the Title of the event if the date that I set occurs today - so If i add an event called “Monday Morning Breakfast” and I give it a day of Monday morning (Im using a date picker and date object), I want it to appear on Monday.

I can return all events if it is before todays and after today but I cannot make evens thats only exists for today.

I already have the dates and am using moment - I want to return the document based on if the date object = today.

Then calculate the time for the beginning of today (i.e. midnight) and compare the dates in your collection to that.

var date = moment().startOf('day');
Events.find({'startTime':{'$gte':date}})
2 Likes

To me isSomething is a boolean, like Meteor.isClient for example. If someone else reads your code, or if yourself will read your code in a few months, I think it might be a good practice to rename your variable from isToday to today. Just my two cents…

Ohhh ok - since the datepicker is also passing the actual time with it - this is why its not working, because im saying get me all the documents that are that this very specific date / time stamp.

Is that the correct logic? Im only two weeks into meteor so your explanation is of help.

A datetime object is basically just a number - usually the amount of seconds since a particular date (like 1970-01-01 00:00:00). Thus, if you want everything from today, you need to pass in the datetime from this morning (i.e. it’s a discrete number which the Date class will then work with) and every datetime later than that is simply a larger number.

That’s about it, really.

Thus your original code indeed would have only gotten Events which had the exact same timestamp, down to the second.

This makes calculating time differentials easy. Everything else is slightly more work intensive.

It’s also why you don’t want to roll your own - because converting that number into an actual, human-readable timestamp is complicated. Leap years, leap seconds, timezones…

1 Like

Thank you again - its make sense now - I only want the events between midnight and midnight of a day - thanks you again.

The docs are pretty clear that you pass in a second parameter to specify granularity.

If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter.

moment('2010-10-20').isAfter('2010-01-01', 'year'); // false
moment('2010-10-20').isSame('2009-12-31', 'year');  // false

For your case you would pass ‘day’ as the second parameter.