'Check if page creation date is within the last 7 days with Eleventy and Nunjucks?

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!



Solution 1:[1]

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 %}

Solution 2:[2]

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

Solution 3:[3]

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 ``
        }

    });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Lesha Ogonkov
Solution 3 kuwts