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:

  • a: It is the Javascript date object.
  • b: It is the javascript date object.

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

Below given are a few examples of the above function.



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.