Browse Source

add more date range options.

main
Gisle Aune 4 years ago
parent
commit
077ed17046
  1. 64
      svelte-ui/src/components/DateRangeSelect.svelte
  2. 16
      svelte-ui/src/utils/time.ts

64
svelte-ui/src/components/DateRangeSelect.svelte

@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import { onMount } from "svelte"; import { onMount } from "svelte";
import { endOfMonth, endOfWeek, endOfYear, formatFormTime, lastMonth, monthName, nextMonth, startOfMonth, startOfWeek, startOfYear } from "../utils/time";
import { endOfDay, endOfMonth, endOfWeek, endOfYear, formatFormTime, lastMonth, lastYear, monthName, nextMonth, nextYear, startOfDay, startOfMonth, startOfWeek, startOfYear } from "../utils/time";
import EveryMinute from "./EveryMinute.svelte"; import EveryMinute from "./EveryMinute.svelte";
interface DateOption { interface DateOption {
@ -32,8 +32,8 @@ import EveryMinute from "./EveryMinute.svelte";
$: { $: {
if (options.length === 0) { if (options.length === 0) {
let current = startOfMonth(now); let current = startOfMonth(now);
const nextWeek = new Date(Date.now() + (86400000 * 7));
const lastWeek = new Date(Date.now() - (86400000 * 7));
const nextWeek = new Date(now.getTime() + (86400000 * 7));
const lastWeek = new Date(now.getTime() - (86400000 * 7));
options.push({ options.push({
id: "this_week", id: "this_week",
@ -48,6 +48,12 @@ import EveryMinute from "./EveryMinute.svelte";
from: startOfWeek(nextWeek), from: startOfWeek(nextWeek),
to: endOfWeek(nextWeek), to: endOfWeek(nextWeek),
}); });
options.push({
id: "next_7days",
label: "Next 7 Days",
from: startOfDay(now),
to: endOfDay(new Date(now.getTime() + 86400000 * 7)),
});
} }
options.push({ options.push({
id: "last_week", id: "last_week",
@ -55,6 +61,12 @@ import EveryMinute from "./EveryMinute.svelte";
from: startOfWeek(lastWeek), from: startOfWeek(lastWeek),
to: endOfWeek(lastWeek), to: endOfWeek(lastWeek),
}); });
options.push({
id: "last_7days",
label: "Last 7 Days",
from: startOfDay(new Date(now.getTime() - 86400000 * 7)),
to: endOfDay(now),
});
options.push({ options.push({
id: "this_month", id: "this_month",
@ -69,6 +81,18 @@ import EveryMinute from "./EveryMinute.svelte";
from: nextMonth(current), from: nextMonth(current),
to: endOfMonth(nextMonth(current)), to: endOfMonth(nextMonth(current)),
}); });
options.push({
id: "next_30days",
label: "Next 30 Days",
from: startOfDay(now),
to: endOfDay(new Date(now.getTime() + 86400000 * 30)),
});
options.push({
id: "next_90days",
label: "Next 90 Days",
from: startOfDay(now),
to: endOfDay(new Date(now.getTime() + 86400000 * 90)),
});
} }
options.push({ options.push({
id: "last_month", id: "last_month",
@ -76,6 +100,18 @@ import EveryMinute from "./EveryMinute.svelte";
from: lastMonth(current), from: lastMonth(current),
to: endOfMonth(lastMonth(current)), to: endOfMonth(lastMonth(current)),
}); });
options.push({
id: "last_30days",
label: "Last 30 Days",
from: startOfDay(new Date(now.getTime() - 86400000 * 30)),
to: endOfDay(now),
});
options.push({
id: "last_90days",
label: "Last 90 Days",
from: startOfDay(new Date(now.getTime() - 86400000 * 90)),
to: endOfDay(now),
});
options.push({ options.push({
id: "this_year", id: "this_year",
@ -87,15 +123,27 @@ import EveryMinute from "./EveryMinute.svelte";
options.push({ options.push({
id: "next_year", id: "next_year",
label: "Next Year", label: "Next Year",
from: startOfYear(new Date(Date.now() + (366.25 * 86400000))),
to: endOfYear(new Date(Date.now() + (365.25 * 86400000))),
from: startOfYear(nextYear(now)),
to: endOfYear(nextYear(now, 2)),
});
options.push({
id: "next_1year",
label: "Next 1 Year",
from: startOfDay(now),
to: endOfDay(nextYear(now)),
}); });
} }
options.push({ options.push({
id: "last_year", id: "last_year",
label: "Last Year", label: "Last Year",
from: startOfYear(new Date(Date.now() - (365.25 * 86400000))),
to: endOfYear(new Date(Date.now() - (365.25 * 86400000))),
from: startOfYear(lastYear(now)),
to: endOfYear(lastYear(now)),
});
options.push({
id: "last_1year",
label: "Last 1 Year",
from: startOfDay(lastYear(now)),
to: endOfDay(now),
}); });
options.push({ options.push({
@ -104,6 +152,8 @@ import EveryMinute from "./EveryMinute.svelte";
from: null, from: null,
to: null, to: null,
}); });
console.log(options);
} }
} }

16
svelte-ui/src/utils/time.ts

@ -56,6 +56,14 @@ export function endOfMonth(now: Date): Date {
return new Date(nextMonth(now).getTime() - 60000) return new Date(nextMonth(now).getTime() - 60000)
} }
export function nextYear(now: Date, n?: number): Date {
return new Date(now.getFullYear() + (n || 1), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds());
}
export function lastYear(now: Date): Date {
return new Date(now.getFullYear() - 1, now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds());
}
export function nextMonth(now: Date): Date { export function nextMonth(now: Date): Date {
let result: Date; let result: Date;
if (now.getMonth() == 11) { if (now.getMonth() == 11) {
@ -86,6 +94,14 @@ export function endOfWeek(now: Date): Date {
return new Date(startOfWeek(now).getTime() + (86400000 * 6) + (3600000 * 23) + (60000 * 59)) return new Date(startOfWeek(now).getTime() + (86400000 * 6) + (3600000 * 23) + (60000 * 59))
} }
export function startOfDay(now: Date): Date {
return new Date(Math.floor(now.getTime() / 86400000) * 86400000 - (now.getTimezoneOffset() * -60000));
}
export function endOfDay(now: Date): Date {
return new Date(startOfDay(now).getTime() + (3600000 * 23) + (60000 * 59))
}
export function monthName(date: Date): string { export function monthName(date: Date): string {
return monthNames[date.getMonth()] return monthNames[date.getMonth()]
} }
Loading…
Cancel
Save