Open In App

D3.js | d3.timeYear Function

The d3.timeYear function in D3.js is used to return all the years with date in the given range of start and end date.

Syntax:



d3.timeYear.range(start, end, step);

Parameters: This function accept three parameters which are given below:

Return Value: This function returns all the possible years in the given range.



Below programs illustrate the d3.timeYear function in D3.js:

Example 1:




<!DOCTYPE html>
<html>
   <head>
      <title>
          D3.js | d3.timeYear Function
      </title>
      <script src =
          "https://d3js.org/d3.v4.min.js">
      </script>
   </head>
   
   <body>
         
      <script>
         
         // Initialising start and end date
         var start = new Date(2015, 01, 01);
         var end = new Date(2020, 01, 01);
           
         // Calling the timeYear function
         // without step value
         var a = d3.timeYear.range(start, end);
           
         // Getting the years values
         console.log(a);
      </script>
   </body>
</html>

Output:

["2015-12-31T18:30:00.000Z","2016-12-31T18:30:00.000Z",
 "2017-12-31T18:30:00.000Z","2018-12-31T18:30:00.000Z",
 "2019-12-31T18:30:00.000Z"]

Example 2:




<!DOCTYPE html>
<html>
   <head>
      <title>
          D3.js | d3.timeYear Function
      </title>
      <script src =
          "https://d3js.org/d3.v4.min.js">
      </script>
   </head>
   
   <body>
         
      <script>
         
         // Initialising start and end date
         var start = new Date(2015, 01, 01);
         var end = new Date(2020, 01, 01);
           
         // Calling the timeYear function
         // with step value
         var a = d3.timeYear.range(start, end, 2);
           
         // Getting the years values
         console.log(a);
      </script>
   </body>
</html>

Output:

["2015-12-31T18:30:00.000Z","2017-12-31T18:30:00.000Z",
 "2019-12-31T18:30:00.000Z"]

Reference: https://devdocs.io/d3~5/d3-time#timeYear


Article Tags :