-
Hi 👋, Software on the server is executed with For example when computing I think generally this is expected behaviour and makes a lot of sense, but in my case I'd prefer having some kind of computation that aligns between clients and servers. It seems to me that the ways to get this would be:
So my understanding is that I cannot easily escape Browsers applying their knowledge of DST to dates. I did also not find specific tools for this in date-fns so far. I've also had a look at date-fns/utc in hope that it might aid me but this seems to not be the case. Because of this I've been looking into building an const addDuration = (date, delta) => {
const { years = 0, months = 0, weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0 } = delta
const dateWithMonths = add(date, { months, years })
const tzDeltaMonths = date.getTimezoneOffset() - dateWithMonths.getTimezoneOffset()
const dateWithDays = add(dateWithMonths, { days, weeks })
const tzDeltaDays = dateWithMonths.getTimezoneOffset() - dateWithDays.getTimezoneOffset()
return add(dateWithDays, { hours, minutes: minutes + tzDeltaMonths + tzDeltaDays, seconds })
} I've been comparing it with console.table(
examples.map(({ start, delta, utc }) => {
const add1 = add(new Date(start), delta)
const ok1 = add1.toISOString() === utc ? '✅' : '❌'
const add2 = addDuration(new Date(start), delta)
const ok2 = add2.toISOString() === utc ? '✅' : '❌'
return { start: new Date(start), delta, utc: new Date(utc), add1, ok1, add2, ok2 }
}),
) Full example here: example.zip Running the example with Running the example with So… looking at this right now I'm wondering:
I think my example is not quite working as I'd hoped yet - I'll be fiddling with it a little more, but thought it also helpful to seek conversation. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I've also had a look at https://www.npmjs.com/package/date-fns-tz but no luck with it. I've also asked a related question on StackOverflow: https://stackoverflow.com/questions/77488561/adding-durations-to-dates-in-a-manner-stable-across-timezones |
Beta Was this translation helpful? Give feedback.
-
I think I've found a solution to what I was looking for: export const addDuration = (date: Date, delta: Duration): Date => {
const { years = 0, months = 0, weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0 } = delta
const utcYears = date.getUTCFullYear()
const utcMonths = date.getUTCMonth()
const utcDays = date.getUTCDate()
const utcHours = date.getUTCHours()
const utcMinutes = date.getUTCMinutes()
const utcSeconds = date.getUTCSeconds()
const utcMilliseconds = date.getUTCMilliseconds()
return new Date(
Date.UTC(
utcYears + years,
utcMonths + months,
utcDays + weeks * 7 + days,
utcHours + hours,
utcMinutes + minutes,
utcSeconds + seconds,
utcMilliseconds,
),
)
} This seems to add Durations in UTC just fine and appears to be working across clients. |
Beta Was this translation helpful? Give feedback.
I think I've found a solution to what I was looking for: