Jump to content

JS edit to add 'This is a past event' message to all past events

Go to solution Solved by paul2009,

Recommended Posts

Site URL: https://dev-sioux-city-public-museum.squarespace.com

https://dev-sioux-city-public-museum.squarespace.com/exhibitions/a-life-in-the-wild-t-d-mangelsen

Password: scpm

I found some fabulous code on a blog (sorry, I can't find it to reference!) which allowed me to add these 'This a past event' messages on any events which have already passed.

However, the Java script which takes note of the date is using the date the event started, instead of the date the event finished. For example - this event runs Nov 27th - March 31st - I want the message to kick in on April 1st, but it's been showing since Nov 27th.

This is the code I'm using:

<!-- PAST EVENTS MESSAGE -->
<script>
  // Change past events styling//
  if ( document.body.classList.contains('collection-type-events-stacked') && document.body.classList.contains('view-item') ) {
 
    // Get the date of the event from the datetime attribute of the event-date element
    var eventDate = document.getElementsByClassName("event-date")[0].getAttribute("datetime");
    // Get today's date
    var expDate = new Date();
    // Convert the dates to numbers, for simplicity
    var eventDateParsed = Date.parse(eventDate);
    var expDateParsed = Date.parse(expDate);
    // Add one day (in milliseconds) to today's date
    // This ensures that the class isn't added until the event has ended
    expDateParsed = expDateParsed + (24*60*60*1000);
    // Finally, calculate whether the date is in the past or present/future and apply the appropriate class to the body
    if ( eventDateParsed < expDateParsed ) {
        document.body.classList.add("pastEvent");
    } else {
        document.body.classList.add("futureEvent");
    }
  }
</script>

 

image.png

Edited by kellyhutsbydesigns
Link to comment
  • kellyhutsbydesigns changed the title to JS edit to add 'This is a past event' message to all past events
  • Solution
4 hours ago, kellyhutsbydesigns said:

the Java script which takes note of the date is using the date the event started, instead of the date the event finished.

The JS is looking for the first element with the class event-date (the start date) but it needs to look for the second element with this class (the end date). You can do this by changing the line:

var eventDate = document.getElementsByClassName("event-date")[0].getAttribute("datetime");

to

var eventDate = document.getElementsByClassName("event-date")[1].getAttribute("datetime");

Did this help? Please give feedback by clicking an icon below  ⬇️

About me: I've been a SQSP User for 18 yrs. I was invited to join the Circle when it launched in 2016. I have been a Circle Leader since 2017. I don't work for Squarespace. I value honesty, transparency, diversity and good design ♥.
Work: I founded and run SF.DIGITAL, building Squarespace Extensions to supercharge your commerce website. 
Content: Views and opinions are my own. Links in my posts may refer to SF.DIGITAL products or may be affiliate links.
Forum advice is free. You can thank me by clicking one of the feedback emojis below. Coffee is optional.

Link to comment

Hi, I was just curious where you inserted the JS code. Is it into the Code Inject section in the Event List Settings -> Advanced -> Page Header Code Injection ? It seems like a really useful bit of code but I put it in, but it didn't seemed to work.

Link to comment
On 3/23/2022 at 6:05 AM, dzak said:

Hi, I was just curious where you inserted the JS code. Is it into the Code Inject section in the Event List Settings -> Advanced -> Page Header Code Injection ? It seems like a really useful bit of code but I put it in, but it didn't seemed to work.

Settings > Advanced > Code Injection > Footer

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.