{"id":13753017,"url":"https://github.com/edent/Dynamic-SVG-Calendar-Icon","last_synced_at":"2025-05-09T20:34:41.662Z","repository":{"id":50272954,"uuid":"122960681","full_name":"edent/Dynamic-SVG-Calendar-Icon","owner":"edent","description":"Here it is, an SVG calendar which always display's today's date.","archived":false,"fork":false,"pushed_at":"2022-01-13T14:39:54.000Z","size":11,"stargazers_count":196,"open_issues_count":1,"forks_count":29,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-04T19:16:05.558Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edent.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"edent","patreon":"edent","open_collective":"edent","ko_fi":"edent","tidelift":null,"community_bridge":null,"custom":"https://www.amazon.co.uk/hz/wishlist/ls/13GFCFR2B2IX4?tag=edentgithub-21"}},"created_at":"2018-02-26T11:33:18.000Z","updated_at":"2025-03-28T00:16:22.000Z","dependencies_parsed_at":"2022-08-25T11:11:09.612Z","dependency_job_id":null,"html_url":"https://github.com/edent/Dynamic-SVG-Calendar-Icon","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edent%2FDynamic-SVG-Calendar-Icon","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edent%2FDynamic-SVG-Calendar-Icon/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edent%2FDynamic-SVG-Calendar-Icon/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edent%2FDynamic-SVG-Calendar-Icon/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edent","download_url":"https://codeload.github.com/edent/Dynamic-SVG-Calendar-Icon/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253321810,"owners_count":21890470,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T09:01:14.591Z","updated_at":"2025-05-09T20:34:39.511Z","avatar_url":"https://github.com/edent.png","language":null,"funding_links":["https://github.com/sponsors/edent","https://patreon.com/edent","https://opencollective.com/edent","https://ko-fi.com/edent","https://www.amazon.co.uk/hz/wishlist/ls/13GFCFR2B2IX4?tag=edentgithub-21"],"categories":["Others"],"sub_categories":[],"readme":"# This SVG always shows today's date\n\n[Originally posted on my blog](https://shkspr.mobi/blog/2018/02/this-svg-always-shows-todays-date/).\n\nFor [my contact page](https://edent.tel/), 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.\n\nHere it is, \u003ca href=\"https://shkspr.mobi/svg/calendar.svg\"\u003ean SVG calendar which always display's today's date\u003c/a\u003e.\n\n(Note - GitHub doesn't allow data URIs or iFrames - so you'll need to [click the link to see it](https://shkspr.mobi/svg/calendar.svg))\n\nThe background image is derived from the [Twitter TweMoji Calendar icon](https://github.com/twitter/twemoji/blob/gh-pages/2/svg/1f4c5.svg) - CC-BY.\n\n[![Buy me a coffee](https://www.ko-fi.com/img/donate_sm.png)](https://ko-fi.com/edent)\n\n## Use\nSVG supports JavaScript. Browsers will only run the JavaScript if the SVG is included...\n\n* in an iframe `\u003ciframe src=\"calendar.svg\"\u003e\u003c/iframe\u003e`\n* as inline `\u003cbody\u003e\u003csvg\u003e.....\u003c/svg\u003e\u003c/body\u003e`\n\nIt will not run JavaScript in an `\u003cimg\u003e` element.\n\n## HOWTO\n\nText support in SVG is a little awkward, so let me explain how I did this.\n\nSVG supports JavaScript. This will run as soon as the image is loaded.\n\n```\n\u003csvg onload=\"init(evt)\" xmlns=\"http://www.w3.org/2000/svg\"\naria-label=\"Calendar\" role=\"img\" viewBox=\"0 0 512 512\"\u003e\n```\n\nNext step is to get the various date strings. I'm using the `en-GB` locale as that's where I'm based.\n\n```\n\u003cscript type=\"text/ecmascript\"\u003e\u003c![CDATA[\nfunction init(evt) {\n  var time = new Date();\n  var locale = \"en-gb\";\n```\n\nI want to display something like \"Sunday 25 FEB\" - the locale options allow for short and long names. So you could have \"SUN 25 February\".\n\n```\n  var DD   = time.getDate();\n  var DDDD = time.toLocaleString(locale, {weekday: \"long\"});\n  var MMM = time.toLocaleString(locale,  {month:   \"short\"});\n```\n\nFinally, we need to add the text on to the image.\n```  \n  var svgDocument = evt.target.ownerDocument;\n\n  var dayNode = svgDocument.createTextNode(DD);\n  svgDocument.getElementById(\"day\").appendChild(dayNode);\n\n  var weekdayNode = svgDocument.createTextNode(DDDD);\n  svgDocument.getElementById(\"weekday\").appendChild(weekdayNode);\n\n  var monthNode = svgDocument.createTextNode(MMM.toUpperCase());\n  svgDocument.getElementById(\"month\").appendChild(monthNode);\n  \n}\n]]\u003e\u003c/script\u003e\n```\n\nText positioning is relatively simplistic.  An X \u0026 Y position which is anchored to the *bottom* of the text - remember that letters with descenders like `g` 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. \n\nA monospace font makes it easier to predict the layout.\n```\n\u003ctext id=\"month\"\n  x=\"32\" \n  y=\"164\" \n  fill=\"#fff\" \n  font-family=\"monospace\"\n  font-size=\"140px\"\n  style=\"text-anchor: left\"\u003e\u003c/text\u003e\n```\n\nA word on anchoring.  To centre the anchor, use `style=\"text-anchor: middle\"`\n\nA quick test shows that this works on all desktop browsers and Android browsers. I've not tested on iPhones or anything more exotic.\n\nEnjoy!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedent%2FDynamic-SVG-Calendar-Icon","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedent%2FDynamic-SVG-Calendar-Icon","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedent%2FDynamic-SVG-Calendar-Icon/lists"}