Formatting Date

 I wanted to have different formatting for the date and time, so I added something for it.

In edit HTML, up near the <body> tag, just after the class conditions, I put:


<b:if cond='not data:view.isPage'>
<script>
//<![CDATA[
function formatTime(isoString) {
  const date = new Date(isoString);
  let hours = date.getHours();
  const minutes = date.getMinutes();
  const ampm = hours >= 12 ? 'PM' : 'AM';
  hours = hours % 12;
  hours = hours ? hours : 12;
  const minutesStr = minutes < 10 ? '0' + minutes : minutes;
  const formattedTime = hours + ':' + minutesStr + ' ' + ampm;
  return formattedTime;
}
//]]>
</script>
</b:if>


and in the Blog1 widget, inside the <b:includable id='postTimestamp'>

just before the </b:if> is:


      <br/>
      <script>
document.write(formatTime(&#39;<data:post.date.iso8601/>&#39;));
      </script>

If this is done, the time format in settings should be set to show only the date without the time.


 

Comments (2)