检查页面创建日期是否在过去的7天内,高高的和nunjucks?
几天前,我发布了有关检查新创建的帖子的方法,但我在这方面取得了进展,但由于短码对我来说仍然有些新。这是我创建的快捷代码是为了从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将每个日期转换为秒(使用
date()。getTime()
),然后检查差异是否大于1周内的毫秒数(1000 * 60 * 24 * 7
)例如:
或在您的示例中
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:
Or in your example,
我想适当的解决方案是每天安排一次静态网站的生成,以获取这项工作。
I guess proper solution is to schedule generation of your static site for once a day, to get this works.
最终找到了一个解决方案,Eleventy.js当前有点货物,因此每次进行更改时,您都必须重新加载服务器,这让我有些混乱。无论如何,这是短代码:
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: