Skip to content

Commit

Permalink
Add setToday method
Browse files Browse the repository at this point in the history
  • Loading branch information
meghanmae committed Oct 25, 2024
1 parent 315ad1b commit a7d354a
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
>
<template v-slot:actions>
<v-btn
@click="modelValue = new Date()"
@click="setToday"
:class="showTime ? '' : 'mr-10'"
>
Today
Expand Down Expand Up @@ -169,6 +169,7 @@ import {
startOfDay,
endOfDay,
startOfHour,
set,
} from "date-fns";
import { format, toZonedTime, fromZonedTime } from "date-fns-tz";
import {
Expand Down Expand Up @@ -593,6 +594,34 @@ function emitInput(value: Date | null) {
}
}
function setToday() {
const now = new Date();
const today = startOfDay(now);
// Preserve existing time if it's set
if (internalValueZoned.value) {
const existingDate = internalValueZoned.value;
const newDate = set(today, {
hours: existingDate.getHours(),
minutes: existingDate.getMinutes(),
seconds: existingDate.getSeconds(),
milliseconds: existingDate.getMilliseconds(),
});
if (internalTimeZone.value) {
// Adjust for the specified time zone
modelValue.value = fromZonedTime(newDate, internalTimeZone.value);
} else {
// No time zone specified, use the new date as is
modelValue.value = newDate;
}
} else {
// No existing date/time, just set to start of today
modelValue.value = today;
}
}
function close() {
menu.value = false;
}
Expand Down

0 comments on commit a7d354a

Please sign in to comment.