When the browser’s timezone and the tenant’s timezone differ, any date math that runs in browser time can put things on the wrong day. In Guardix, a scheduling and time-clock SaaS for security-guard companies, that showed up as shifts in the wrong column of the week calendar, date pickers selecting the day before, and payroll missing hours from the last day of a period. The fix was the same idea each time: treat a calendar day as a date string in the tenant’s timezone, and only turn it into a Date where you need an exact instant.
How it showed up
Each tenant sets its own timezone, and the customers were US companies spread across several of them. The person looking at the dashboard isn’t always in the company’s timezone, and I usually wasn’t either.
The reports came in over a couple of months:
- Adding a shift for the 13th in the web dashboard saved it on the 12th, at the same time.
- A time card showed the 25th for an account in another timezone, while the photo the guard took had the 26th stamped on it.
- In the Run Payroll dialog, picking a from and a to date selected the day before each one.
- On a newer dashboard’s date range filter, choosing April 1 selected March 31.
- In the week calendar, a shift appeared under the wrong day when the browser’s timezone wasn’t the tenant’s.
They looked like separate bugs, and for a while I fixed them one dashboard at a time.
The week calendar used two timezones at once
This one had a clean root cause, so it’s the best example. The week view built its seven days with date-fns:
startOfWeek(date)reads theDatein the browser’s timezone, so the week was anchored to the browser’s midnight.- The column headers used
format(date, "EEE")andgetDate(date), also in browser time.
The function that decides whether a shift belongs on a day compared dates formatted in the tenant’s timezone. So the days were built in one timezone and the shifts were matched in another. When the two timezones agreed, everything lined up. When they didn’t, shifts slid into the neighbouring column.
A shared helper had the same flaw. todayInTimezone(timezone) called setHours(0, 0, 0, 0) to get midnight, but setHours works in the browser’s timezone. “Midnight in the tenant’s timezone” was really midnight wherever the browser happened to be.
The fix: date strings, a noon anchor, then tenant midnight
The helper now gets today’s date as a string in the tenant’s timezone and converts that string to the tenant’s midnight:
export function todayInTimezone(timezone: string): Date {
const dateStr = formatInTimeZone(new Date(), timezone, "yyyy-MM-dd")
return fromZonedTime(new Date(`${dateStr}T00:00:00`), timezone)
}
The week view does its arithmetic on a noon anchor and keeps the days as ISO date strings:
const refDateStr = formatISODate(startDate ?? new Date(), tenantTimezone)
const refNoon = new Date(`${refDateStr}T12:00:00`)
const weekStartNoon = startOfWeek(refNoon, { weekStartsOn })
const weekDateStrs = Array.from({ length: 7 }, (_, i) =>
format(addDays(weekStartNoon, i), "yyyy-MM-dd"),
)
const weekDates = weekDateStrs.map((d) =>
fromZonedTime(new Date(`${d}T00:00:00`), tenantTimezone),
)
Noon is the point of the exercise. A browser-local noon on a given date is still that calendar date in any timezone, so startOfWeek and addDays can’t push it into the day before or after.
The strings drive everything the user sees: the column headers, the tooltips, the drag-and-drop target IDs and the “today” highlight, which is now a plain string comparison against today’s date in the tenant’s timezone. The Date values, set to tenant midnight, are only used to match shifts to days. The schedule view and the schedule navigation called the same helper, so they got the fix without changes of their own.
The date pickers
The pickers had a wider version of the problem. The web app had five different date widgets plus native <input type="date"> and <input type="datetime-local"> fields, spread across roughly 80 places. A datetime-local input shows time in the browser’s timezone, and the time-clock edit dialogs used it, which made it a payroll bug waiting to happen.
I consolidated them into three widgets: a date picker for form fields like date of birth or hire date, one range picker, and a date-time picker that works in the tenant’s timezone. The time-clock dialogs use that last one now. The native inputs and the duplicate range pickers were deleted, and two small helpers convert between a date string and a browser-local midnight, so every picker turns a picked day into a string the same way.
Log, list and report filters now also default to yesterday and today.
Payroll
Payroll had its own version. Hours were grouped by UTC date, which dropped hours from the last day of a pay period for tenants with negative UTC offsets. Every US timezone has one. The calculation now runs on the tenant’s local date.
What to check in your own app
- Search for
setHours(0, 0, 0, 0),startOfDay,startOfWeekandgetDateon values that are meant to live in a timezone other than the browser’s. They all work in browser time. - Look for a component that builds dates in one timezone and compares them in another. The mismatch is the bug, more than either side of it.
- Keep calendar days as
yyyy-MM-ddstrings for display and comparison. Convert to aDateonly when you need an instant. - Don’t use
datetime-localfor times that belong to a timezone the user’s browser isn’t in. - Group hours and money by the tenant’s local date, not the UTC date.
- Test with the browser set to a different timezone from the tenant. My last check on these fixes was a table of how the same data looked from several timezones.