Open In App

D3.js interpolateDate() Function

The interpolateDate() function in D3.js is used to return the interpolator function that returns the date based on the value given in the interpolator function. This function takes two parameters of the Javascript date object.

Syntax:



interpolateDate(a, b);

Parameters: It takes two parameters:

Returns: It returns the interpolator function of the two dates given.



Below given are a few examples of the above function.

Example 1:




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" 
        content="width=device-width, 
                 initial-scale=1.0">
  <title>Document</title>
</head>
<style>
</style>
<body>
  <!--Fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src
  </script>
  <script>
    try{
     // Trying Simple date string
      console.log(
d3.interpolateDate("01/01/2001", "01/02/2002")(0.26))
      // Given end date only
      console.log(
d3.interpolateDate(new Date(), new Date("01/01/2001"))(0.5))
      // When both start and end date is given
      console.log(
d3.interpolateDate(new Date("04/01/2001"), new Date("01/01/2001"))(2))
      console.log(typeof d3.interpolateDate("2 asda", "13 geeks"))
    }
    catch(err){
      throw err;
    }
      
  </script>
</body>
</html>

Output:

Example 2:




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" 
        content="width=device-width,
                 initial-scale=1.0">
  <title>Document</title>
</head>
<style>
</style>
<body>
  <!--Fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js">
  </script>
  <script>
    let startDate=new Date()
    console.log("Start date: ", startDate);
    let endDate=new Date();
    endDate=endDate.setDate(endDate.getDate() - 3);
    console.log("End date: ", endDate);
    console.log(
d3.interpolateDate(startDate, endDate)(1)) 
  </script>
</body>
</html>

Output:


Article Tags :