JavaScript Get Current Year 2 and 4 Digit – Example

JavaScript Get Current Year 2 and 4 Digit – Example

javascript get current year 2, 4 digit; In this tutorial, we will learn how to get current year two digits in javascript.

Before we get two digits of the current year, get four digits from date and get four digits of the current year, we know about the getFullYear() javascript method, which is used to get the year of the specified date.

JavaScript getFullYear() Method

The getFullYear() method of the javascript method, which is used to get the year of the specified date. It will return year to four digits for dates between the year 1000 and 9999.

Syntax

Date.getFullYear();

Examples

Here we will take an example like, how to get current year 2 digit, how to get the last two digits of year javascript, how to get current year 4 digits javascript and How to get javascript get 4 digit year from date.

1. get current year 4 digit javascript

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript get current year 4 digit</title>
</head>
<body>
  <script type = "text/javascript">
     var date = new Date(); // date object
     var getYear =  date.getFullYear(); // get current year
     document.write( "javascript get current year 4 digit is :- " + getYear ); 
  </script>  
</body>
</html>

Result of the above code is:

javascript get current year 4 digit

2. javascript get current year 2 digit

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript get current year 2 digit</title>
</head>
<body>
  dd/mm/yyyy to mm/dd/yyyy
  <script type = "text/javascript">
     var date = new Date(); // date object
     var getYear =  date.getFullYear(); // get current year
     var getTwodigitYear = getYear.toString().substring(2); // get last two digits from year
     document.write( "javascript get current year 2 digit is :- " + getTwodigitYear ); 
  </script>  
</body>
</html>

Result of the above code is:

javascript get current year 2 digit dd/mm/yyyy to mm/dd/yyyy

3. javascript get 4 digit year from date

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript get 4 digit year from date</title>
</head>
<body>
  <script type = "text/javascript">
     var date = new Date("Mon Nov 25 2019 22:37:25"); // date object
     var getYear =  date.getFullYear(); // get current year
     document.write( "javascript get 4 digit year from date :- " + getYear ); 
  </script>  
</body>
</html>

Result of the above code is:

javascript get 4 digit year from date

4. get current year and month in javascript

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>get current year and month in javascript</title>
</head>
<body>
  <script type = "text/javascript">
    function month2digits(month)
    { 
        return (month < 10 ? '0' : '') + month;
    }

    var date = new Date(); // date object
    var getCurrentYear = date.getFullYear(); // get current year javascript
    document.write( "get current year in javascript is :- " + getCurrentYear ); 
    document.write( "<br>" ); 

    var month2digits = month2digits(date.getMonth()+1); // get month in two digits
    document.write( "javascript get current month 2 digits is :- " + month2digits ); 

  </script>  
</body>
</html>

Result of the above code is:

get current year and month in javascript

If you want to know more about javascript date and time methods, you may like following date and time methods:

You may like

  1. Get Current Date Time javaScript
  2. JavaScript: Date setUTCDate() Method
  3. Set UTC Month In javaScript
  4. setUTCFullYear() Method JavaScript With Examples
  5. javascript date setUTCHours() Method With Examples
  6. JavaScript: d.setUTCMinutes() Method e.g.
  7. setUTCSecond() Method By JavaScript
  8. javaScript Date set UTC milliseconds
  9. setUTCSecond() Method By JavaScript
  10. JavaScript: Date.getUTCDate() Method
  11. getUTCmonth Method javaScript With Example
  12. Digital Clock with date in javaScript
  13. JavaScript: Set Date Methods
  14. JavaScript Get Date Methods

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *