1164

How do I get the current year in JavaScript?

Michał Perłakowski
  • 70,955
  • 24
  • 137
  • 155
Satch3000
  • 40,202
  • 83
  • 203
  • 337
  • 14
    Astonishing if there really isn't a duplicate for this. [This one](http://stackoverflow.com/questions/4562587/shortest-way-to-print-current-year-in-javascript) is close, but not exact. – T.J. Crowder May 14 '11 at 14:02
  • 4
    Note that the "current" year is not necessarily the same for everyone on the planet at the same time. One has to consider time zone. Most of the answers here give the current year in the user's local time zone (assuming the code is running in a web browser). – Matt Johnson-Pint Jun 06 '17 at 16:30
  • Does this answer your question? [Shortest way to print current year in a website](https://stackoverflow.com/questions/4562587/shortest-way-to-print-current-year-in-a-website) – Aryan Beezadhur Jan 01 '21 at 18:48
  • So everyone look out for those 24 hours out of 8760 where this is not true! My uncle lost a toe not heeding this advice! – Dave Kanter Mar 09 '21 at 00:11

9 Answers9

2041

Create a new Date() object and call getFullYear():

new Date().getFullYear()
// returns the current year

Hijacking the accepted answer to provide some basic example context like a footer that always shows the current year:

<footer>
    &copy; <span id="year"></span>
</footer>

Somewhere else executed after the HTML above has been loaded:

<script>
    document.getElementById("year").innerHTML = new Date().getFullYear();
</script>

document.getElementById("year").innerHTML = new Date().getFullYear();
footer {
  text-align: center;
  font-family: sans-serif;
}
<footer>
    &copy; <span id="year">2018</span> by FooBar
</footer>
Edward Karak
  • 10,340
  • 8
  • 41
  • 68
Jason Harwig
  • 36,877
  • 5
  • 40
  • 42
  • 30
    For other functions in the same family, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Getter – user456584 Feb 13 '14 at 18:16
  • To get the year according to universal time use [getUTCFullYear()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCFullYear) – Courtney Pattison Dec 20 '17 at 18:41
  • 2
    The footer example above would be more clear if it were just:
    – Mike Jun 01 '19 at 03:38
  • Answer: By upvoting accepted answer 9 more times :) – s3c Apr 26 '21 at 11:16
239
// Return today's date and time
var currentTime = new Date()

// returns the month (from 0 to 11)
var month = currentTime.getMonth() + 1

// returns the day of the month (from 1 to 31)
var day = currentTime.getDate()

// returns the year (four digits)
var year = currentTime.getFullYear()

// write output MM/dd/yyyy
document.write(month + "/" + day + "/" + year)
Michel Ayres
  • 5,357
  • 8
  • 55
  • 92
Sourav
  • 15,195
  • 32
  • 97
  • 152
  • 1
    Links: [getFullYear()](http://www.w3schools.com/jsref/jsref_getfullyear.asp) | [getDate()](http://www.w3schools.com/jsref/jsref_getdate.asp) | [getMonth()](http://www.w3schools.com/jsref/jsref_getmonth.asp) – Michel Ayres Mar 14 '14 at 17:56
  • 3
    Use getFullYear() instead of getYear(). See http://stackoverflow.com/questions/16296897/whats-the-difference-between-javascripts-getyear-and-getfullyear-functions – roland Apr 25 '15 at 05:13
103

Here is another method to get date

new Date().getDate()          // Get the day as a number (1-31)
new Date().getDay()           // Get the weekday as a number (0-6)
new Date().getFullYear()      // Get the four digit year (yyyy)
new Date().getHours()         // Get the hour (0-23)
new Date().getMilliseconds()  // Get the milliseconds (0-999)
new Date().getMinutes()       // Get the minutes (0-59)
new Date().getMonth()         // Get the month (0-11)
new Date().getSeconds()       // Get the seconds (0-59)
new Date().getTime()          // Get the time (milliseconds since January 1, 1970)
Parth Jasani
  • 1,900
  • 1
  • 18
  • 24
7

Such is how I have it embedded and outputted to my HTML web page:

<div class="container">
    <p class="text-center">Copyright &copy; 
        <script>
            var CurrentYear = new Date().getFullYear()
            document.write(CurrentYear)
        </script>
    </p>
</div>

Output to HTML page is as follows:

Copyright © 2018

  • You shouldn't use document.write() but instead something like innerHTML of a span – leonheess Feb 05 '19 at 12:31
  • The main thing here is the var CurrentYear = new Date().getFullYear(). The document.write() is secondary, and of course various other solutions exist. You should not mark down my reputation for this. – Jerusalem Programmer Feb 21 '19 at 09:31
  • 6
    I disagree. This is obviously a beginner question and we should not teach bad practice to anyone especially those who are beginning to learn the fundamentals. Additionally your answer does not provide any new information as `new Date().getFullYear()` is already featured in the accepted answer. – leonheess Feb 21 '19 at 12:38
  • 3
    1.) Bad practice says you, but others disagree since this is valid vanilla Javascript; 2.) My answer provides new information of how to implement this within an HTML page, and not just console.log(). – Jerusalem Programmer Mar 14 '19 at 02:51
  • *Valid* as in *working* not *valid* as in *good*. See the accepted answer for the correct way of doing this – leonheess Mar 14 '19 at 07:14
  • 2
    "Good" is subjective. There is more than one way to skin a cat, and more than one way to solve coding problems. If the solution is simple and works, then why is this not "good" in your humble opinion? – Jerusalem Programmer Mar 15 '19 at 12:00
  • 1
    Well, sir, it is clear that this is not a clear-cut objective standard (and indeed is an open debate), as there are many who disagree with you: https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice ...and also here: https://stackoverflow.com/questions/18789190/why-not-document-write Therefore, your marking my reputation down for your subjective opinion is not very nice since there are others who would disagree with you. Remember the main point here, and that I just provided a simple way to print this to HTML file instead of console.log(). – Jerusalem Programmer Mar 16 '19 at 16:49
  • 1
    Please see the previous two links where you can see HUNDREDS of people who disagree with you. There are also HUNDREDS of people who agree with you, so it is not a clear-cut issue. You insistence upon your way or the high way is quite closed-minded since you cannot be certain if someone may decide that document.write() is the best solution for his/her needs. – Jerusalem Programmer Mar 17 '19 at 07:17
6

for current year we can use getFullYear() from Date class however there are many function which you can use as per the requirements, some functions are as,

var now = new Date()
console.log("Current Time is: " + now);

// getFullYear function will give current year 
var currentYear = now.getFullYear()
console.log("Current year is: " + currentYear);

// getYear will give you the years after 1990 i.e currentYear-1990
var year = now.getYear()
console.log("Current year is: " + year);

// getMonth gives the month value but months starts from 0
// add 1 to get actual month value 
var month = now.getMonth() + 1
console.log("Current month is: " + month);

// getDate gives the date value
var day = now.getDate()
console.log("Today's day is: " + day);
Rupesh Agrawal
  • 387
  • 7
  • 21
4

You can simply use javascript like this. Otherwise you can use momentJs Plugin which helps in large application.

new Date().getDate()          // Get the day as a number (1-31)
new Date().getDay()           // Get the weekday as a number (0-6)
new Date().getFullYear()      // Get the four digit year (yyyy)
new Date().getHours()         // Get the hour (0-23)
new Date().getMilliseconds()  // Get the milliseconds (0-999)
new Date().getMinutes()       // Get the minutes (0-59)
new Date().getMonth()         // Get the month (0-11)
new Date().getSeconds()       // Get the seconds (0-59)
new Date().getTime()          // Get the time (milliseconds since January 1, 1970)

function generate(type,element)
{
 var value = "";
 var date = new Date();
 switch (type) {
  case "Date":
   value = date.getDate();  // Get the day as a number (1-31)
   break;
  case "Day":
   value = date.getDay();  // Get the weekday as a number (0-6)
   break;
  case "FullYear":
   value = date.getFullYear(); // Get the four digit year (yyyy)
   break;
  case "Hours":
   value = date.getHours(); // Get the hour (0-23)
   break;
  case "Milliseconds":
   value = date.getMilliseconds(); // Get the milliseconds (0-999)
   break;
  case "Minutes":
   value = date.getMinutes();     // Get the minutes (0-59)
   break;
  case "Month":
   value = date.getMonth(); // Get the month (0-11)
   break;
  case "Seconds":
   value = date.getSeconds(); // Get the seconds (0-59)
   break;
  case "Time":
   value = date.getTime();  // Get the time (milliseconds since January 1, 1970)
   break;
 }

 $(element).siblings('span').text(value);
}
li{
  list-style-type: none;
  padding: 5px;
}

button{
  width: 150px;
}

span{
  margin-left: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<ul>
 <li>
  <button type="button" onclick="generate('Date',this)">Get Date</button>
  <span></span>
 </li>
 <li>
  <button type="button" onclick="generate('Day',this)">Get Day</button>
  <span></span>
 </li>
 <li>
  <button type="button" onclick="generate('FullYear',this)">Get Full Year</button>
  <span></span>
 </li>
 <li>
  <button type="button" onclick="generate('Hours',this)">Get Hours</button>
  <span></span>
 </li>
 <li>
  <button type="button" onclick="generate('Milliseconds',this)">Get Milliseconds</button>
  <span></span>
 </li>

 <li>
  <button type="button" onclick="generate('Minutes',this)">Get Minutes</button>
  <span></span>
 </li>
 <li>
  <button type="button" onclick="generate('Month',this)">Get Month</button>
  <span></span>
 </li>
 <li>
  <button type="button" onclick="generate('Seconds',this)">Get Seconds</button>
  <span></span>
 </li>
 <li>
  <button type="button" onclick="generate('Time',this)">Get Time</button>
  <span></span>
 </li>
</ul>
Nishal K.R
  • 910
  • 5
  • 19
3

Take this example, you can place it wherever you want to show it without referring to script in the footer or somewhere else like other answers

<script>new Date().getFullYear()>document.write(new Date().getFullYear());</script>

Copyright note on the footer as an example

Copyright 2010 - <script>new Date().getFullYear()>document.write(new Date().getFullYear());</script>
Majali
  • 362
  • 3
  • 8
  • 1
    This is an unnecessary answer. – Crocsx Sep 20 '19 at 06:16
  • @crocsx you can add this code in your HTML in the place you want to show it without referring to script in the footer or somewhere else like all other answers – Majali Sep 20 '19 at 06:23
  • is the same as the answer of Jerusalem Programmer slightly different – Crocsx Sep 20 '19 at 06:25
  • @Crocsx this answer is cleaner does not include HTML element and CSS classes that assume you use bootstrap, it's only one line, right? – Majali Sep 20 '19 at 06:28
  • the question is just get the year in JS, not get the year and place it in the footer. And inn that case, you could have edited the other answer. Anyway... – Crocsx Sep 20 '19 at 06:30
  • Place in the footer is just an example, it could be placed anywhere when needed – Majali Sep 20 '19 at 06:36
  • I looks a lot like what can be found on this website : http://updateyourfooter.com/ – Seblor Sep 20 '19 at 07:02
  • Those examples are old, if you look at first one it includes type="text/javascript" which is not needed anymore and if you look at there next example it includes 2010&& which is not needed, right? I think I need to remove copyright 2010 - to make the answer more straight forward I'll make the edit now – Majali Sep 20 '19 at 07:11
1

TL;DR

Most of the answers found here are correct only if you need the current year based on your local machine's time zone and offset (client side) - source which, in most scenarios, cannot be considered reliable (beause it can differ from machine to machine).

Reliable sources are:

  • Web server's clock (but make sure that it's updated)
  • Time APIs & CDNs

Details

A method called on the Date instance will return a value based on the local time of your machine.

Further details can be found in "MDN web docs": JavaScript Date object.

For your convenience, I've added a relevant note from their docs:

(...) the basic methods to fetch the date and time or its components all work in the local (i.e. host system) time zone and offset.

Another source mentioning this is: JavaScript date and time object

it is important to note that if someone's clock is off by a few hours or they are in a different time zone, then the Date object will create a different times from the one created on your own computer.

Some reliable sources that you can use are:

But if you simply don't care about the time accuracy or if your use case requires a time value relative to local machine's time then you can safely use Javascript's Date basic methods like Date.now(), or new Date().getFullYear() (for current year).

Community
  • 1
  • 1
Consta Gorgan
  • 471
  • 6
  • 17
1

Instantiate the class Date and call upon its getFullYear method to get the current year in yyyy format. Something like this:

let currentYear = new Date().getFullYear;

The currentYear variable will hold the value you are looking out for.

YDF
  • 55
  • 7