Jump to content

Custom Code für 24h Format

Recommended Posts

Dear Community, 

I have a question regarding the 24h format in the event calendar. I was able to accomplish this using the following code.

<script>
(function () {
  if (window.NodeList && !NodeList.prototype.forEach) {
    NodeList.prototype.forEach = Array.prototype.forEach;
  }

  if (document.readyState == 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }

  function init() {
    var elements = document.querySelectorAll('.event-time-12hr-start, .event-time-12hr-end, .event-time-12hr');
    if(elements) {
      elements.forEach(function (el) {

        var time = el.textContent;

        if (time.length < 9) {

        timenew = time.replace("PM","");
        timenew = timenew.replace("AM","");
        dateold = new Date("2013/01/01 " + time);
        datenew = new Date("2013/01/01 " + timenew);

        if (time.indexOf("PM")) {
            if (dateold.getHours() > 12) {
            datenew.setHours (datenew.getHours() + 12);
            }
        }
        n = datenew.getMinutes();
        el.textContent = (datenew.getHours() + ":" + (n < 10 ? '0' + n : n));
    }
      });
    }
  }
})();
</script>

Now, unfortunately, the problem is that this only works on desktop devices (laptop/PC etc..). As soon as I open the page on a mobile device, the time switches to (AM/PM). Would someone here possibly have advice on how I could change this by means of code? 

And would it also be possible to enter the start time without end time in the time setting of the events? 

Kind regards and thanks for your help. 
Christian Bucher

Screenshot_20230922_102010_Chrome.jpg

Link to comment
  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Posted Images

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.