Erick Engelhardt
Erick Engelhardt

Reputation: 734

D3 time formatting locale

Is possible to return localized information from D3 Time Format?

d3.time.format('%B %Y')(new Date(d))

This always return EN values.

Upvotes: 4

Views: 6635

Answers (1)

xZ6a33YaYEfmv
xZ6a33YaYEfmv

Reputation: 1816

Yes, it's possible. There is a github wiki page here.

Basically you need to use d3.locale(definition) method. Definition is an object like this:

{
  "decimal": ".",
  "thousands": ",",
  "grouping": [3],
  "currency": ["$", ""],
  "dateTime": "%a %b %e %X %Y",
  "date": "%m/%d/%Y",
  "time": "%H:%M:%S",
  "periods": ["AM", "PM"],
  "days": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
  "shortDays": ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
  "months": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  "shortMonths": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
}

Update

The formatting of numbers, dates and currencies varies by language and locale. While the default build of D3 is intended for U.S. English, you can change the behavior of D3’s formatters by loading new locales as needed.

So, locale is not gets identified automatically and you should load desired by yourself.

Little example:

var ua = d3.locale(<uk_UA definition>); //you need this object to be available everywhere this locale is required
ua.timeFormat('%B %Y'); //use this instead of d3.time.format('%B %Y')

Upvotes: 5

Related Questions