kellyhutsbydesigns Posted March 22, 2022 Share Posted March 22, 2022 (edited) 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> Edited March 22, 2022 by kellyhutsbydesigns Link to comment
Solution paul2009 Posted March 22, 2022 Solution Share Posted March 22, 2022 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 ⬇️ kellyhutsbydesigns and dzak 1 1 About: SQSP User for 17 yrs. Circle Leader since 2017. I value honesty, transparency, diversity and good design ♥. Work: Founder of SF Digital, building Squarespace Extensions to supercharge your commerce website. Content: Links in my posts may refer to SF Digital products or may be affiliate links. If my advice helped, you can thank me by clicking one of the feedback emojis below. I love coffee too. Link to comment
kellyhutsbydesigns Posted March 22, 2022 Author Share Posted March 22, 2022 Fabulous! Thank you so much! paul2009 1 Link to comment
dzak Posted March 22, 2022 Share Posted March 22, 2022 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
tuanphan Posted March 24, 2022 Share Posted March 24, 2022 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 dzak 1 Email me if you have need any help (free, of course.). Answer within 24 hours. Or send to forum message How to: Setup Password & Share url - Insert Custom CSS - Page Header - Upload Custom Font - Upload File - Find Block ID - Contact Customer Care Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment