Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function autoTimeFormat(date: Date) {
const formatMillisecond = timeFormat('.%L'),
formatSecond = timeFormat(':%S'),
formatMinute = timeFormat('%I:%M'),
formatHour = timeFormat('%I %p'),
formatDay = timeFormat('%a %d'),
formatWeek = timeFormat('%b %d'),
formatMonth = timeFormat('%B'),
formatYear = timeFormat('%Y');
return (timeSecond(date) < date ? formatMillisecond
: timeMinute(date) < date ? formatSecond
: timeHour(date) < date ? formatMinute
: timeDay(date) < date ? formatHour
: timeMonth(date) < date ? (timeWeek(date) < date ? formatDay : formatWeek)
: timeYear(date) < date ? formatMonth
: formatYear)(date);
}
const customTickFormat = (date, index, allDates) => {
let format;
if (d3timeDay(date) < date) {
format = '%I %p';
} else if (d3timeMonth(date) < date) {
format = d3timeWeek(date) < date ? '%a %d' : '%b %d';
} else if (d3timeYear(date) < date) {
if (index === 0) {
format = '%b %Y';
} else {
format = (allDates.length >= 15 || window.innerWidth < 1024) ? '%b' : '%B';
}
} else {
format = '%Y';
}
return d3timeFormat(format)(date);
};
export function multiDateFormat(date) {
let formatter;
if (timeSecond(date) < date) {
formatter = formatMillisecond;
} else if (timeMinute(date) < date) {
formatter = formatSecond;
} else if (timeHour(date) < date) {
formatter = formatMinute;
} else if (timeDay(date) < date) {
formatter = formatHour;
} else if (timeMonth(date) < date) {
if (timeWeek(date) < date) {
formatter = formatDay;
} else {
formatter = formatWeek;
}
} else if (timeYear(date) < date) {
formatter = formatMonth;
} else {
formatter = formatYear;
}
return formatter(date);
}