Skip to content

Instantly share code, notes, and snippets.

@alvinwan
Last active August 14, 2021 22:46
Show Gist options
  • Save alvinwan/9ccf06916caa8583be5ca5c1995cc2a1 to your computer and use it in GitHub Desktop.
Save alvinwan/9ccf06916caa8583be5ca5c1995cc2a1 to your computer and use it in GitHub Desktop.
getCurrentYear

GetCurrentYear.js

  1. Define an element with id currentYear.
  2. Paste the following, after that element
<script>document.querySelector('#currentYear').innerHTML = new Date().getFullYear();</script>

Or the following. I don't see why you would pick the longer one though.

<script src="https://gist.github.com/raw/9ccf06916caa8583be5ca5c1995cc2a1/getCurrentYear.js" type="application/javascript"></script>
/**
* Get current year dynamically, so that you don't have to update the
* year manually on a static website.
**/
(function() {
const defaultId = 'currentYear';
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id') || defaultId;
document.querySelector('#' + id).innerHTML = new Date().getFullYear();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment