Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

101
Vistas
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!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

0

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

    });
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda