0

Keystone.js ブログがあり、Wordpress の /archive/year/month のようなブログ アーカイブを追加したいと考えています。投稿オブジェクトに日付フィールドを追加しましたが、公開日を使用してこれを行う方法があると思います。

現在、アーカイブの年は「2014」、アーカイブの月は「06」ですが、「-publishedDate」の値は次のようになります"publishedDate" : Date( 1355644800000 )。クエリに関数を記述して、日付を JS 日付オブジェクトとして解析し、値を一致させる方法はありますか?

// Load the posts
view.on('init', function(next) {

    var q = keystone.list('Post').paginate({
            page: req.query.page || 1,
            perPage: 10,
            maxPages: 10
        })
        .where('state', 'published')
        .sort('-publishedDate')
        .populate('author categories');

    if (locals.data.category) {
        q.where('categories').in([locals.data.category]);
    }

            // If archive section, filter by year and month
            if (locals.data.archiveYear && locals.data.archiveMonth) {
        q.where('-publishedDate',locals.data.archiveYear);
                    q.where('-publishedDate',locals.data.archiveMonth);
    }

    q.exec(function(err, results) {
        locals.data.posts = results;
        next(err);
    });

});
4

2 に答える 2

1

user1572796 よりもmoment.js に似たコードを使用する

if (locals.filters.year) {
   var start = moment().year(locals.filters.year).month(locals.filters.month).startOf('month');
   var end = moment().year(locals.filters.year).month(locals.filters.month).endOf('month');

   q.where('publishedDate', { $gt: start, $lt: end });
}
于 2014-10-30T20:25:41.017 に答える