Open In App

D3.js now() function

The d3.now() function in D3.js is used to return the current time according to the performance.now() function available in the javascript or If this is not available than date.now() function is used to return the current time.

Syntax:



d3.now()

Parameters: It does not take any parameters.

Return Value: It returns the time of the dataType string.



Note: The output may vary on different machines.

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 = "https://d3js.org/d3.v4.min.js">
  </script>
  <script>
    console.log("Current time using d3.now(): ")
    console.log(d3.now())
    console.log(d3.now())
    console.log(d3.now())
    console.log(d3.now())
    console.log(d3.now())
    console.log(d3.now())
    console.log(d3.now())
  </script>
</body>
</html>

Output:

Example 2: Note the difference in performance.now() and d3.now().




<!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>
    console.log("Current time using d3.now(): ")
    console.log(d3.now())
    console.log("Current time using performance.now(): ")
    console.log(performance.now())
      
    console.log("Current time using performance.now(): ")
    console.log(performance.now())
    console.log("Current time using d3.now(): ")
    console.log(d3.now())
  </script>
</body>
</html>

Output:


Article Tags :