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-
Asked
Active
Viewed 2,131 times
1
-
Seriously! This question is 3 years old – RobM Sep 12 '18 at 18:01
5 Answers
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
-
1I 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
-
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