<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/rss-style.xsl" type="text/xsl"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	     xmlns:dc="http://purl.org/dc/elements/1.1/"
	   xmlns:atom="http://www.w3.org/2005/Atom"
	     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>This SVG always shows today&#8217;s date &#8211; Terence Eden’s Blog</title>
	<atom:link href="https://shkspr.mobi/blog/2018/02/this-svg-always-shows-todays-date/feed/" rel="self" type="application/rss+xml" />
	<link>https://shkspr.mobi/blog</link>
	<description>Regular nonsense about tech and its effects 🙃</description>
	<lastBuildDate>Tue, 25 Feb 2025 09:46:36 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://shkspr.mobi/blog/wp-content/uploads/2023/07/cropped-avatar-32x32.jpeg</url>
	<title>This SVG always shows today&#8217;s date &#8211; Terence Eden’s Blog</title>
	<link>https://shkspr.mobi/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title><![CDATA[This SVG always shows today's date]]></title>
		<link>https://shkspr.mobi/blog/2018/02/this-svg-always-shows-todays-date/</link>
					<comments>https://shkspr.mobi/blog/2018/02/this-svg-always-shows-todays-date/#comments</comments>
				<dc:creator><![CDATA[@edent]]></dc:creator>
		<pubDate>Sun, 25 Feb 2018 16:07:13 +0000</pubDate>
				<category><![CDATA[/etc/]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[svg]]></category>
		<guid isPermaLink="false">https://shkspr.mobi/blog/?p=29122</guid>

					<description><![CDATA[For my contact page, I wanted a generic calendar icon to let people view my diary.  Calendar icons are almost always a skeuomorph of a paper calendar, but I wondered if I could make it slightly more useful by creating a dynamic icon.  Here it is, an SVG calendar which always display&#039;s today&#039;s date:    The background image is derived from the Twitter TweMoji Calendar icon - CC-BY.  Text support in …]]></description>
										<content:encoded><![CDATA[<p>For <a href="https://edent.tel/">my contact page</a>, I wanted a generic calendar icon to let people view my diary.  Calendar icons are almost always a skeuomorph of a paper calendar, but I wondered if I could make it slightly more useful by creating a <em>dynamic</em> icon.</p>

<p>Here it is, <a href="https://shkspr.mobi/svg/calendar.svg">an SVG calendar which always display's today's date</a>:</p>

<iframe style="background: #fff; border:none;" src="https://shkspr.mobi/svg/calendar.svg" width="256px" height="256px"></iframe>

<p><small>The background image is derived from the <a href="https://github.com/twitter/twemoji/blob/gh-pages/2/svg/1f4c5.svg">Twitter TweMoji Calendar icon</a> - CC-BY.</small></p>

<p>Text support in SVG is a little awkward, so let me explain how I did this.</p>

<p>SVG supports JavaScript. This will run as soon as the image is loaded.</p>

<pre><code class="language-svg">&lt;svg onload="init(evt)" xmlns="http://www.w3.org/2000/svg"
aria-label="Calendar" role="img" viewBox="0 0 512 512"&gt;
</code></pre>

<p>Next step is to get the various date strings. I'm using the <code>en-GB</code> locale as that's where I'm based.</p>

<pre><code class="language-svg">&lt;script type="text/ecmascript"&gt;&lt;![CDATA[
function init(evt) {
  var time = new Date();
  var locale = "en-gb";
</code></pre>

<p>I want to display something like "Sunday 25 FEB" - the locale options allow for short and long names. So you could have "SUN 25 February".</p>

<pre><code class="language-js">  var DD   = time.getDate();
  var DDDD = time.toLocaleString(locale, {weekday: "long"});
  var MMM = time.toLocaleString(locale,  {month:   "short"});
</code></pre>

<p>Finally, we need to add the text on to the image.</p>

<pre><code class="language-js">  var svgDocument = evt.target.ownerDocument;

  var dayNode = svgDocument.createTextNode(DD);
  svgDocument.getElementById("day").appendChild(dayNode);

  var weekdayNode = svgDocument.createTextNode(DDDD);
  svgDocument.getElementById("weekday").appendChild(weekdayNode);

  var monthNode = svgDocument.createTextNode(MMM.toUpperCase());
  svgDocument.getElementById("month").appendChild(monthNode);

}
]]&gt;&lt;/script&gt;
</code></pre>

<p>Text positioning is relatively simplistic.  An X &amp; Y position which is anchored to the <em>bottom</em> of the text - remember that letters with descenders like <code>g</code> will extend beyond the bottom of the Y co-ordinate.  This is also where we set the colour of the text, its size, and a font.</p>

<p>A monospace font makes it easier to predict the layout.</p>

<pre><code class="language-svg">&lt;text id="month"
  x="32" 
  y="164" 
  fill="#fff" 
  font-family="monospace"
  font-size="140px"
  style="text-anchor: left"&gt;&lt;/text&gt;
</code></pre>

<p>A word on anchoring.  To centre the anchor, use <code>style="text-anchor: middle"</code></p>

<p>A quick test shows that this works on all desktop browsers and Android browsers. I've not tested on iPhones or anything more exotic.</p>

<p>Enjoy!</p>

<hr>

<ul>
<li><a href="https://github.com/edent/Dynamic-SVG-Calendar-Icon">GitHub repo</a></li>
<li><a href="https://news.ycombinator.com/item?id=16459550">Discussion on HackerNews</a></li>
</ul>
<img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=29122&HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager">]]></content:encoded>
					
					<wfw:commentRss>https://shkspr.mobi/blog/2018/02/this-svg-always-shows-todays-date/feed/</wfw:commentRss>
			<slash:comments>20</slash:comments>
		
		
			</item>
	</channel>
</rss>
