UNPKG

bananas-commerce-admin

Version:

What's this, an admin for apes?

39 lines (33 loc) 1.4 kB
import { Granularity } from "../types/dashboard"; export function iso8601Week(value: Date) { // Create a copy of the current date, we don't want to mutate the original const date = new Date(value.getTime()); // Find Thursday of this week starting on Monday date.setDate(date.getDate() + 4 - (date.getDay() || 7)); const thursday = date.getTime(); // Find January 1st date.setMonth(0); // January date.setDate(1); // 1st const jan1st = date.getTime(); // Round the amount of days to compensate for daylight saving time const days = Math.round((thursday - jan1st) / 86400000); // 1 day = 86400000 ms return Math.floor(days / 7) + 1; } export function getDateValueFormatter(granularity: Granularity) { // TODO: Add date range args to allow less verbose labels, e.g. only show year when // range span multiple years return (value: Date): string => { switch (granularity) { case "day": return value.toLocaleDateString("default", { month: "short", day: "numeric" }); case "week": return `v${iso8601Week(value)}\n${value.getFullYear().toString()}`; case "month": return value.toLocaleString("default", { month: "short" }); case "quarter": return `Q${Math.floor(value.getMonth() / 3) + 1}\n${value.getFullYear().toString()}`; case "year": return value.getFullYear().toString(); } }; }