Skip to content

Instantly share code, notes, and snippets.

@capaj
Created October 19, 2020 12:34
Show Gist options
  • Save capaj/36b42160ef246ec944a1fdd2105ae833 to your computer and use it in GitHub Desktop.
Save capaj/36b42160ef246ec944a1fdd2105ae833 to your computer and use it in GitHub Desktop.
import moment from 'moment-timezone'
export const shiftUtcDayTimeToTimezone = (
utcDayTime: string,
timezone: string
) => {
const offsetMinutes = moment.tz(moment.utc(), timezone).utcOffset()
const [hours, minutes] = utcDayTime.split(':')
const quotient = Math.floor(offsetMinutes / 60)
const remainder = offsetMinutes % 60
return `${Number(hours) + quotient}:${Number(minutes) + remainder}`
}
describe('shiftDayTimeToTimezone', () => {
it('should shift', async () => {
expect(
shiftUtcDayTimeToTimezone('08:40', 'Europe/Prague')
).toMatchInlineSnapshot(`"10:40"`)
expect(
shiftUtcDayTimeToTimezone('8:40', 'Europe/Prague')
).toMatchInlineSnapshot(`"10:40"`)
expect(
shiftUtcDayTimeToTimezone('08:40', 'Europe/London')
).toMatchInlineSnapshot(`"9:40"`)
expect(
shiftUtcDayTimeToTimezone('08:40', 'America/New_York')
).toMatchInlineSnapshot(`"4:40"`)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment