How to change the date formatting on blog posts - Squarespace 7.1
If you want to display the blog post formatting inside of a blog post on your Squarespace site, you are limited as to how the date presents itself.
Using the code below, add this to Code Injection > Footer to change the date format.
Within the code, it’s this text in bold that you will want to change. There is a list of possible date options below
const dateformat = "dd mmm yyyy";
You can add hyphens in-between the letters to then create a date format like so as an alternative option.
dd-mm-yyyy
Note:
You won’t see changes made to the site until you press “Save” within Code Injection.
<!-- Fix inconsistent blog date format on Squarespace 7.1 template --> <script src="https://stevenlevithan.com/assets/misc/date.format.js"></script> <script> (function(){ document.addEventListener('DOMContentLoaded', function() { const dateformat = "dd mmm yyyy"; const pubdates = document.querySelectorAll("time[datetime]:not([class*=event-time]), time[pubdate], time.blog-meta-item--date"); pubdates.forEach(pubDate => { let d = new Date(pubDate.getAttribute("datetime") || pubDate.innerText); if (pubDate.classList.contains('blog-meta-item--date') ) { d = new Date(document.querySelector('[itemprop="datePublished"]').getAttribute('content').split("T")[0]); } pubDate.innerHTML = d.format(dateformat); }); }); })(); </script>
Format | Output | Description |
---|---|---|
YY | 18 | Two-digit year |
YYYY | 2018 | Four-digit year |
M | 1-12 | The month, beginning at 1 |
MM | 01-12 | The month, 2-digits |
MMM | Jan-Dec | The abbreviated month name |
MMMM | January-December | The full month name |
D | 1-31 | The day of the month |
DD | 01-31 | The day of the month, 2-digits |
d | 0-6 | The day of the week, with Sunday as 0 |
dd | Su-Sa | The min name of the day of the week |
ddd | Sun-Sat | The short name of the day of the week |
dddd | Sunday-Saturday | The name of the day of the week |
H | 0-23 | The hour |
HH | 00-23 | The hour, 2-digits |
h | 1-12 | The hour, 12-hour clock |
hh | 01-12 | The hour, 12-hour clock, 2-digits |
m | 0-59 | The minute |
mm | 00-59 | The minute, 2-digits |
s | 0-59 | The second |
ss | 00-59 | The second, 2-digits |
SSS | 000-999 | The millisecond, 3-digits |
Z | =+05:00 | The offset from UTC, ±HH:mm |
ZZ | 500 | The offset from UTC, ±HHmm |
A | AM PM | |
a | am pm |