當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


d3.js interpolateDate()用法及代碼示例

D3.js中的interpolateDate()函數用於返回插值器函數,該函數根據插值器函數中給出的值返回日期。此函數采用Javascript日期對象的兩個參數。

用法:

interpolateDate(a, b);

參數:它帶有兩個參數:

  • a:它是Javascript日期對象。
  • b:它是javascript日期對象。

返回:它返回給定兩個日期的內插函數。

下麵給出了上述函數的一些示例。



範例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> 
    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>

輸出:

範例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>

輸出:




相關用法


注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js interpolateDate() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。