[D3] D3 and JS Dates and Times

JavaScript Date Object

// 不同的形式
var dateObject = new Date();
var dateObject = new Date(value);
var dateObject = new Date(dateString);
var dateObject = new Date(year, month, day[, hour, minute, second, millisecond]);

D3 Time Formatting

d3.time.format(specifier);

// 範例1:
var dateToday = new Date();
var formatter = d3.time.format("%A");
formatter(dateToday);
// 範例2:
var dateFuture = new Date(2020, 0, 12);

dateFuture;
// 輸出: Sun Jan 12 2020 00:00:00 GMT+0800 (CST)

typeof(dateFuture);
// 輸出:"object"

dateFuture.getFullYear();
// 輸出:2020

var dateFormatter1 = d3.time.format("%A");
dateFormatter1(dateFuture);
// 輸出:"Sunday"

var dateFormatter2 = d3.time.format("%B");
dateFormatter2(dateFuture);
// 輸出:"January"

var dateFormatter3 = d3.time.format("%m");
dateFormatter3(dateFuture);
// 輸出:"01"

var dateFormatter4 = d3.time.format("%p");
dateFormatter4(dateFuture);
// 輸出:"AM"

您可在此處留言

avatar

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料

  Subscribe  
Notify of