Jump to content

Translating months or showing only the numbers??

Recommended Posts

HELP!

I'm having problems with date. My site is in Finnish, and I don't want to hide months. I'd like to show a number instead of month: e.g. 12.3.2021. How I can make this happen?

Blog summary

blockId":"6fef3f5ae4cc9f0a1af1

blogs

<time class="Blog-meta-item Blog-meta-item--date" datetime="2020-12-08" id="yui_3_17_2_1_1613466147605_439">8 December 2020</time>

and blog page

<time class="Blog-meta-item Blog-meta-item--date" datetime="2020-12-08">8 December 2020</time>

Best regards,

Katja

Link to comment
  • Replies 4
  • Views 757
  • Created
  • Last Reply
Quote

<script>
  document.addEventListener("DOMContentLoaded", function(event) {
    //Figure out context (blog-post or not?)
    var dateMeta = document.querySelector('[itemprop=datePublished]');
    if (dateMeta != null) {
      //Handle blog post date
      var publishedDate = new Date(dateMeta.content);
      var dateElem = document.querySelector('time span');
      updateDateElement(dateElem, publishedDate);
    }
    else {
      //Handle all dates on page
      var times = document.querySelectorAll('time');
      for (var i = 0; i < times.length; i++) {
        var dateElem = times[i];
        var publishedDate = new Date(dateElem.innerHTML);
        updateDateElement(dateElem, publishedDate);
      }
    }
  });
  
  function updateDateElement(elem, date) {
    //Construct new display string
    //If you want a different localization, you can change the 'en-US'.
    //You can also type in undefined insteath of 'en-US' and let the device choose.
    var dateFormat = { year: 'numeric', month: 'numeric', day: 'numeric' };
    var newFormat = date.toLocaleDateString('en-GB', dateFormat);
    
    //Assign new string
    elem.textContent = newFormat;
  }
</script>

 

This worked 🙂

Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.