fix: improve date handling in DatePicker and attendance components for better parsing and formatting
This commit is contained in:
parent
ae1cc7b9a9
commit
dd9ce7df85
@ -39,12 +39,17 @@ function DatePicker({
|
|||||||
|
|
||||||
const date = React.useMemo(() => {
|
const date = React.useMemo(() => {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value instanceof Date) {
|
if (value instanceof Date) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(value)) {
|
||||||
|
const [year, month, day] = value.split('-').map(Number);
|
||||||
|
return new Date(year, month - 1, day);
|
||||||
|
}
|
||||||
|
|
||||||
return new Date(value);
|
return new Date(value);
|
||||||
}, [value]);
|
}, [value]);
|
||||||
|
|||||||
@ -14,6 +14,7 @@ import {
|
|||||||
import { useCan } from '@/hooks/use-can';
|
import { useCan } from '@/hooks/use-can';
|
||||||
import { generateRandomColors } from '@/lib/utils';
|
import { generateRandomColors } from '@/lib/utils';
|
||||||
import { formatRupiah } from '@/lib/rupiah';
|
import { formatRupiah } from '@/lib/rupiah';
|
||||||
|
import { format } from 'date-fns';
|
||||||
import { Head, router } from '@inertiajs/react';
|
import { Head, router } from '@inertiajs/react';
|
||||||
import {
|
import {
|
||||||
Banknote,
|
Banknote,
|
||||||
@ -415,7 +416,7 @@ export default function Analysis({
|
|||||||
|
|
||||||
const hasActiveFilters = !!startDate || !!endDate;
|
const hasActiveFilters = !!startDate || !!endDate;
|
||||||
|
|
||||||
const formatDate = useCallback((date: Date): string => date.toISOString().split('T')[0], []);
|
const formatDate = useCallback((date: Date): string => format(date, 'yyyy-MM-dd'), []);
|
||||||
|
|
||||||
const applyFilters = useCallback(() => {
|
const applyFilters = useCallback(() => {
|
||||||
router.get(
|
router.get(
|
||||||
@ -932,7 +933,8 @@ export default function Analysis({
|
|||||||
tickMargin={8}
|
tickMargin={8}
|
||||||
minTickGap={32}
|
minTickGap={32}
|
||||||
tickFormatter={(value) => {
|
tickFormatter={(value) => {
|
||||||
const date = new Date(value);
|
const [year, month, day] = String(value).split('-').map(Number);
|
||||||
|
const date = new Date(year, month - 1, day);
|
||||||
return date.toLocaleDateString('id-ID', { day: 'numeric', month: 'short' });
|
return date.toLocaleDateString('id-ID', { day: 'numeric', month: 'short' });
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -943,7 +945,9 @@ export default function Analysis({
|
|||||||
className="w-[150px]"
|
className="w-[150px]"
|
||||||
nameKey="views"
|
nameKey="views"
|
||||||
labelFormatter={(value) => {
|
labelFormatter={(value) => {
|
||||||
return new Date(value).toLocaleDateString('id-ID', { day: 'numeric', month: 'short', year: 'numeric' });
|
const [year, month, day] = String(value).split('-').map(Number);
|
||||||
|
const date = new Date(year, month - 1, day);
|
||||||
|
return date.toLocaleDateString('id-ID', { day: 'numeric', month: 'short', year: 'numeric' });
|
||||||
}}
|
}}
|
||||||
formatter={(value, name, item, index) => (
|
formatter={(value, name, item, index) => (
|
||||||
<>
|
<>
|
||||||
|
|||||||
@ -470,7 +470,8 @@ export default function AttendanceIndex({
|
|||||||
|
|
||||||
const todayMidnight = new Date();
|
const todayMidnight = new Date();
|
||||||
todayMidnight.setHours(0, 0, 0, 0);
|
todayMidnight.setHours(0, 0, 0, 0);
|
||||||
const cellDate = new Date(cell.date);
|
const [cYear, cMonth, cDay] = String(cell.date).split('-').map(Number);
|
||||||
|
const cellDate = new Date(cYear, cMonth - 1, cDay);
|
||||||
cellDate.setHours(0, 0, 0, 0);
|
cellDate.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
const isPastDate = cellDate < todayMidnight;
|
const isPastDate = cellDate < todayMidnight;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user