BigW Consortium Gitlab

date_time_formatters.js 1.2 KB
Newer Older
1
import { timeFormat as time } from 'd3-time-format';
2
import { timeSecond, timeMinute, timeHour, timeDay, timeWeek, timeMonth, timeYear } from 'd3-time';
3
import { bisector } from 'd3-array';
4

5 6 7 8 9 10 11 12 13 14 15
const d3 = {
  time,
  bisector,
  timeSecond,
  timeMinute,
  timeHour,
  timeDay,
  timeWeek,
  timeMonth,
  timeYear,
};
16

17
export const dateFormat = d3.time('%a, %b %-d');
18 19 20 21 22 23 24
export const timeFormat = d3.time('%-I:%M%p');
export const dateFormatWithName = d3.time('%a, %b %-d');
export const bisectDate = d3.bisector(d => d.time).left;

export function timeScaleFormat(date) {
  let formatFunction;
  if (d3.timeSecond(date) < date) {
25
    formatFunction = d3.time('.%L');
26
  } else if (d3.timeMinute(date) < date) {
27
    formatFunction = d3.time(':%S');
28
  } else if (d3.timeHour(date) < date) {
29
    formatFunction = d3.time('%-I:%M');
30
  } else if (d3.timeDay(date) < date) {
31
    formatFunction = d3.time('%-I %p');
32
  } else if (d3.timeWeek(date) < date) {
33
    formatFunction = d3.time('%a %d');
34
  } else if (d3.timeMonth(date) < date) {
35
    formatFunction = d3.time('%b %d');
36
  } else if (d3.timeYear(date) < date) {
37
    formatFunction = d3.time('%B');
38
  } else {
39
    formatFunction = d3.time('%Y');
40 41 42
  }
  return formatFunction(date);
}