datePresets.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { toIsoDateYmdFromDate } from "@/lib/frontend/search/dateRange";
  2. function toLocalDay(date) {
  3. if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
  4. return new Date();
  5. }
  6. return new Date(date.getFullYear(), date.getMonth(), date.getDate());
  7. }
  8. function addDays(date, deltaDays) {
  9. const d = toLocalDay(date);
  10. d.setDate(d.getDate() + Number(deltaDays || 0));
  11. return d;
  12. }
  13. function startOfMonth(date) {
  14. const d = toLocalDay(date);
  15. return new Date(d.getFullYear(), d.getMonth(), 1);
  16. }
  17. function startOfPrevMonth(date) {
  18. const d = toLocalDay(date);
  19. return new Date(d.getFullYear(), d.getMonth() - 1, 1);
  20. }
  21. function endOfPrevMonth(date) {
  22. const d = toLocalDay(date);
  23. // Day 0 of current month = last day of previous month.
  24. return new Date(d.getFullYear(), d.getMonth(), 0);
  25. }
  26. function startOfYear(date) {
  27. const d = toLocalDay(date);
  28. return new Date(d.getFullYear(), 0, 1);
  29. }
  30. export const DATE_PRESET_KEY = Object.freeze({
  31. TODAY: "today",
  32. YESTERDAY: "yesterday",
  33. LAST_7_DAYS: "last_7_days",
  34. LAST_30_DAYS: "last_30_days",
  35. THIS_MONTH: "this_month",
  36. LAST_MONTH: "last_month",
  37. THIS_YEAR: "this_year",
  38. });
  39. /**
  40. * Build German-labeled date presets for the Search date filter.
  41. *
  42. * All outputs are ISO YYYY-MM-DD strings (local calendar values, no timezone drift).
  43. *
  44. * @param {{ now?: Date }} args
  45. * @returns {Array<{ key: string, label: string, from: string, to: string }>}
  46. */
  47. export function buildDatePresets({ now = new Date() } = {}) {
  48. const today = toLocalDay(now);
  49. const presets = [
  50. {
  51. key: DATE_PRESET_KEY.TODAY,
  52. label: "Heute",
  53. from: today,
  54. to: today,
  55. },
  56. {
  57. key: DATE_PRESET_KEY.YESTERDAY,
  58. label: "Gestern",
  59. from: addDays(today, -1),
  60. to: addDays(today, -1),
  61. },
  62. {
  63. key: DATE_PRESET_KEY.LAST_7_DAYS,
  64. label: "Letzte 7 Tage",
  65. from: addDays(today, -6),
  66. to: today,
  67. },
  68. {
  69. key: DATE_PRESET_KEY.LAST_30_DAYS,
  70. label: "Letzte 30 Tage",
  71. from: addDays(today, -29),
  72. to: today,
  73. },
  74. {
  75. key: DATE_PRESET_KEY.THIS_MONTH,
  76. label: "Dieser Monat",
  77. from: startOfMonth(today),
  78. to: today,
  79. },
  80. {
  81. key: DATE_PRESET_KEY.LAST_MONTH,
  82. label: "Letzter Monat",
  83. from: startOfPrevMonth(today),
  84. to: endOfPrevMonth(today),
  85. },
  86. {
  87. key: DATE_PRESET_KEY.THIS_YEAR,
  88. label: "Dieses Jahr",
  89. from: startOfYear(today),
  90. to: today,
  91. },
  92. ];
  93. return presets.map((p) => ({
  94. key: p.key,
  95. label: p.label,
  96. from: toIsoDateYmdFromDate(p.from),
  97. to: toIsoDateYmdFromDate(p.to),
  98. }));
  99. }