1

I currently have a datestamp after a copyright statment I would like to have the year after the - to be dynamic is there a way to sub in some javascript to write the year in as it changes see below for what i am hoping for

1994-2015 1994-
robm
  • 1,041
  • 1
  • 10
  • 14

5 Answers5

5

True inline solution :

1994-<script type="text/javascript"> document.write(new Date().getFullYear())</script>
Black0ut
  • 1,642
  • 14
  • 28
  • why document.write is considered bad practice: http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – THE AMAZING Sep 01 '15 at 15:54
  • @RichardGrant I rarely use document.write but I agree with the 2nd answer on the link, sometimes it the right tool for the job – Black0ut Sep 01 '15 at 15:59
  • 1
    I agree as-well, and you are using it correctly. I just thought i would mention the issues with it. Depending how the OP's framework is set up Document.write might not work as intended – THE AMAZING Sep 01 '15 at 16:02
  • thanks Black )ut that did the trick! – robm Sep 01 '15 at 16:29
1

Edited for cross browser support

<span>1994-
  <span id='year'>
     <script type='text/javascript'>
      var current_year = (new Date()).getFullYear();
      var year_node = document.getElementById('year');
      year_node.innerHTML = '';
      year_node.appendChild(document.createTextNode(current_year));
    </script>
  </span>
</span>

This grabs the clients local time, if the users time/date is incorrect so will this year output.

THE AMAZING
  • 1,496
  • 2
  • 16
  • 38
0

use js date object:

var year = new Date().getYear();

As per the comment about this method being deprecated, you can also use:

var year = Date().getFullYear();
taxicala
  • 21,408
  • 7
  • 37
  • 66
  • getYear() is deprecated. https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date/getYear – dschu Sep 01 '15 at 15:44
  • you need to instantiate the Date object before you can access child methods in the Object. – THE AMAZING Sep 01 '15 at 16:06
0

Grab your element and append the current year by using the .getFullYear() function.

document.getElementById('years').innerHTML += new Date().getFullYear();
<div>
  <span id="years">1994-</span>
</div>
CDelaney
  • 1,238
  • 4
  • 19
  • 36
0

Quote from MDN

var today = new Date();
var year = today.getFullYear();
dschu
  • 4,992
  • 5
  • 31
  • 48