0

I am looking for a date-fns-tz version of How do I get the UTC time of "midnight" for a given timezone?

Here's the jest test I wrote

  it('should work the way we expect', () => {
    expect(
      getInstantAtMidnightForTimezoneForGivenInstant(1661357361000, 'America/Toronto')
    ).toEqual(parseISO('2022-08-24T04:00:00Z'));

    expect(
      getInstantAtMidnightForTimezoneForGivenInstant(1661357361000, 'Asia/Kolkata')
    ).toEqual(parseISO('2022-08-23T18:30:00Z'));
  });

I so far got this far

export function getInstantAtMidnightForTimezoneForGivenInstant(instant: number| Date, tz: string) : Date {
  var midnightAtTimezoneString = formatInTimeZone(instant, tz, 'yyyy-MM-dd 00:00:00');

  // parse midnightAtTimezoneString but as UTC?
  // return parsed value.
  return new Date();
}
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265

1 Answers1

0
export function getInstantAtMidnightForTimezoneForGivenInstant(instant: number | Date, tz: string): Date {
  const midnightAtTimezoneString = formatInTimeZone(instant, tz, "yyyy-MM-dd'T'00:00:00XXX");
  return toDate(midnightAtTimezoneString)
}
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265