Expiring New Badge/Label

Shows a "New" badge only when the published date in data-expiring-date is within the last 7 days. The badge hides itself automatically once the window expires, with no server-side logic required. Format: D/M/YYYY.

badgelabelcmsjavascriptdatewebflow

Code

index.html
html
<div class="example-card">
  <div class="example-card__start">
    <div class="new-badge__wrapper">
      <div data-expiring-date="22/12/2024" class="new-badge"><span class="new-badge__span">New</span></div>
    </div>
    <h2 class="new-badge__h2">Show<br>New Badge</h2>
  </div>
  <div class="example-card__end">
    <img src="images/image-chair.jpg" loading="lazy" width="540" alt="Image of Chair" class="example-card__img">
  </div>
</div>
styles.css
css
.example-card {
  background-color: #efeeec;
  border-radius: 1.5em;
  width: 30em;
  height: 20em;
  padding: 1.25em;
  display: flex;
}

.example-card__start {
  flex-flow: column;
  justify-content: space-between;
  align-items: flex-start;
  width: 50%;
  display: flex;
}

.example-card__end {
  width: 50%;
}

.example-card__img {
  object-fit: cover;
  border-radius: .5em;
  width: 100%;
  height: 100%;
}

.new-badge {
  background-color: #ff4c24;
  border-radius: .25em;
  padding: .3em .5em .25em;
  display: none;
}

.new-badge__span {
  color: #efeeec;
  font-size: 18px;
  font-weight: 500;
  line-height: 1;
}

.new-badge__h2 {
  text-transform: uppercase;
  margin-top: 0;
  margin-bottom: 0;
  padding: .25em;
  font-size: 2.75em;
  font-weight: 700;
  line-height: .8;
}
script.js
javascript
function initExpiringNewBadge() {
  const elements = document.querySelectorAll('[data-expiring-date]');
  const today = new Date();
  today.setHours(0, 0, 0, 0);

  elements.forEach(el => {
    const dateStr = el.getAttribute('data-expiring-date'); // format D/M/YYYY
    const [day, month, year] = dateStr.split('/').map(Number);

    // Construct date object from attribute (month is zero-based in JS)
    const publishedDate = new Date(year, month - 1, day);
    publishedDate.setHours(0, 0, 0, 0);

    // Calculate the difference in days
    const diffInMs = today - publishedDate;
    const diffInDays = diffInMs / (1000 * 60 * 60 * 24);

    // Show badge if the published date is within the last 7 days (0 to 6 days ago)
    if (diffInDays >= 0 && diffInDays < 7) {
      el.style.display = 'block';
    }
  });
}

// Initialize Expiring New Badge/Label
document.addEventListener('DOMContentLoaded', () => {
  initExpiringNewBadge();
});

Guide

data-expiring-date

Add to the badge element itself with the published date in D/M/YYYY format — e.g. data-expiring-date="22/12/2024". The badge starts hidden (display: none) and is shown by the script only if the date falls within the last 7 days.

Expiry Window

The badge is visible for exactly 7 days starting from the published date (days 0 through 6). On day 7 and beyond it stays hidden. Adjust the < 7 condition in the script to change the window length.

Date Format

The attribute expects D/M/YYYY — single-digit days and months are fine (e.g. 5/3/2025). The script splits on / and constructs a Date object manually, so it is not affected by browser locale differences in date parsing.

Webflow / CMS

When cards are driven by a CMS, bind the Last Published Date field to data-expiring-date and select the D/M/YYYY format. The badge will automatically appear and expire without any code changes.