检查页面创建日期是否在过去的7天内,高高的和nunjucks?

发布于 2025-01-25 16:21:29 字数 852 浏览 4 评论 0原文

几天前,我发布了有关检查新创建的帖子的方法,但我在这方面取得了进展,但由于短码对我来说仍然有些新。这是我创建的快捷代码是为了从Luxon获得今天的日期(然后在7天之前减去以获取上周的日期),然后将其与已通过的日期进行比较。目前,我必须将通过的数据转换为因为它们来自几页。

这是快捷代码:

config.addShortcode("newlyAdded", function(date) { 

let lastWeek = DateTime.now().minus({days: 7}).toFormat('yyyy-MM-dd');

let itemDate = DateTime.fromFormat(date, 'yyyy-MM-dd').toFormat('yyyy-MM-dd');

if(lastWeek <= itemDate) {
    return "
    <span class='my-label uk-margin-small-right uk-margin-small-top tag-featured'>
      <i class='fas fa-star margin-right-5'></i>Newly Added</span>
    "
}
});

然后我试图在宏中使用短代码:

{% newlyAdded post.data.date %}

但是我遇到了这个错误:

"Invalid DateTime" when used in a Nunjucks macro

我觉得解决方案可能很简单,并且与短代码语法或类似的东西有关。

任何建议都非常感谢

I posted a few days ago about having a way to check for newly created posts, and I am making some headway in this but got a little stuck as shortcodes are still a bit new to me. This is the shortcodes that I created to get today's date from Luxon (then subtract by 7 days to get the last week's date) and then compare it with a date that is passed in. At the moment, I have to convert the data passed in as they are coming from a few pages.

this is the shortcode:

config.addShortcode("newlyAdded", function(date) { 

let lastWeek = DateTime.now().minus({days: 7}).toFormat('yyyy-MM-dd');

let itemDate = DateTime.fromFormat(date, 'yyyy-MM-dd').toFormat('yyyy-MM-dd');

if(lastWeek <= itemDate) {
    return "
    <span class='my-label uk-margin-small-right uk-margin-small-top tag-featured'>
      <i class='fas fa-star margin-right-5'></i>Newly Added</span>
    "
}
});

then I am trying to use the shortcode in a macro:

{% newlyAdded post.data.date %}

but I am getting this error:

"Invalid DateTime" when used in a Nunjucks macro

I feel like the solution is probably quite simple and has to do with shortcode syntax or something like that which I am unaware of.

Any and all advice is very appreciated

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小女人ら 2025-02-01 16:21:29

您可以将每个日期转换为秒(使用date()。getTime()),然后检查差异是否大于1周内的毫秒数(1000 * 60 * 24 * 7

例如:

const dateToCheck = new Date(2022, 1, 1, 0);

const diff = new Date().getTime() - new Date(dateToCheck).getTime();

const msInWeek = 1000 * 60 * 24 * 7;

const isOlderThenWeek = diff > msInWeek;

console.log(isOlderThenWeek);

或在您的示例中

const env = nunjucks.configure();

env.addGlobal('isOlderThenWeek', (startDate) => {
    return ((new Date().getTime() - new Date(startDate).getTime()) / 1000) > 604800
});

{% if isOlderThenWeek(inputDate) %}
  This post was added in the last week
{% endif %}

You could convert each date into seconds (with Date().getTime()), then just check if the difference is greater than the number of milliseconds in 1 week (1000 * 60 * 24 * 7)

For example:

const dateToCheck = new Date(2022, 1, 1, 0);

const diff = new Date().getTime() - new Date(dateToCheck).getTime();

const msInWeek = 1000 * 60 * 24 * 7;

const isOlderThenWeek = diff > msInWeek;

console.log(isOlderThenWeek);

Or in your example,

const env = nunjucks.configure();

env.addGlobal('isOlderThenWeek', (startDate) => {
    return ((new Date().getTime() - new Date(startDate).getTime()) / 1000) > 604800
});

{% if isOlderThenWeek(inputDate) %}
  This post was added in the last week
{% endif %}

很酷又爱笑 2025-02-01 16:21:29

我想适当的解决方案是每天安排一次静态网站的生成,以获取这项工作。

I guess proper solution is to schedule generation of your static site for once a day, to get this works.

伏妖词 2025-02-01 16:21:29

最终找到了一个解决方案,Eleventy.js当前有点货物,因此每次进行更改时,您都必须重新加载服务器,这让我有些混乱。无论如何,这是短代码:

config.addNunjucksShortcode("newlyAdded", function (date) {
        let today = DateTime.now().toFormat('yyyy-MM-dd');

        let lastWeek = DateTime.now().minus({
            days: 7
        }).toFormat('yyyy-MM-dd');

        let itemDate = DateTime.fromJSDate(date, 'yyyy-MM-dd').plus({
            days: 1
        }).toFormat('yyyy-MM-dd');

        if (lastWeek <= itemDate) {
            return `
            <span class='my-label tag-seo added'><i class='fas fa-star margin-right-5'></i>New This Week</span>
                `
        } else {
            return ``
        }

    });

Ended up finding a solution, eleventy.js is currently a little buggy so you have to reload the server everytime you make a change and that was messing with me a bit. Anyway this is the shortcode:

config.addNunjucksShortcode("newlyAdded", function (date) {
        let today = DateTime.now().toFormat('yyyy-MM-dd');

        let lastWeek = DateTime.now().minus({
            days: 7
        }).toFormat('yyyy-MM-dd');

        let itemDate = DateTime.fromJSDate(date, 'yyyy-MM-dd').plus({
            days: 1
        }).toFormat('yyyy-MM-dd');

        if (lastWeek <= itemDate) {
            return `
            <span class='my-label tag-seo added'><i class='fas fa-star margin-right-5'></i>New This Week</span>
                `
        } else {
            return ``
        }

    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文