Skip to content

Adding durations in different timezones #3578

Closed Answered by runjak
runjak asked this question in Q&A
Discussion options

You must be logged in to vote

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,
      utcSe…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by runjak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant