Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in doesn't work at all (or at least not as expected). #31

Open
janhesters opened this issue Nov 30, 2024 · 1 comment
Open

in doesn't work at all (or at least not as expected). #31

janhesters opened this issue Nov 30, 2024 · 1 comment

Comments

@janhesters
Copy link

I'm trying to set the time of a date in a specific timezone:

      date: new TZDate('2024-01-15T00:00:00.000Z', 'America/New_York'),
      time: '09:30',
      expected: new TZDate('2024-01-15T09:30:00.000-05:00', 'America/New_York'),

Basically I want a function, that given aTZDate and a time in HH:mm, sets that time on the date, but preserves the date in the timezone.

Here is the code I wrote:

export function combineDateAndTime({
  date,
  time,
}: {
  date: TZDate;
  time: string;
}): TZDate {
  // Validate the input date.
  if (Number.isNaN(date.getTime())) {
    throw new TypeError('Invalid date format');
  }

  // Parse the time string.
  const timeMatch = /^(\d{1,2}):(\d{2})$/.exec(time);
  if (!timeMatch) {
    throw new TypeError('Invalid time format');
  }

  // Validate hours and minutes.
  const hours = Number.parseInt(timeMatch[1], 10);
  const minutes = Number.parseInt(timeMatch[2], 10);
  if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59) {
    throw new TypeError('Invalid time format');
  }

  // Create the new TZDate instance.
  console.log('date', date);
  console.log('date.timeZone', date.timeZone);
  console.log('tz(date.timeZone!)', tz(date.timeZone!));
  const result = setMilliseconds(
    setMinutes(setHours(date, hours, { in: tz(date.timeZone!) }), minutes, {
      in: tz(date.timeZone!),
    }),
    0,
    { in: tz(date.timeZone!) },
  );
  console.log('result', result);
  return result;
}

Actual

date TZDate 2024-01-15T00:00:00.000Z {
  timeZone: 'America/New_York',
  internal: 2024-01-14T19:00:00.000Z
}
date.timeZone America/New_York
tz(date.timeZone!) [Function (anonymous)]
result TZDate 2024-01-14T14:30:00.000Z {
  timeZone: 'America/New_York',
  internal: 2024-01-14T09:30:00.000Z
}

Expected

date TZDate 2024-01-15T00:00:00.000Z {
  timeZone: 'America/New_York',
  internal: 2024-01-14T19:00:00.000Z
}
date.timeZone America/New_York
tz(date.timeZone!) [Function (anonymous)]
result TZDate 2024-01-15T9:30:00.000Z {
  timeZone: 'America/New_York',
  internal: 2024-01-15T14:30:00.000Z
}
@myang5
Copy link

myang5 commented Dec 3, 2024

Having similar issues, in converting code from moment.js that is determining whether multiple dates have the same hour across DST.

// Two dates with different hours in UTC, but should be the same hour
// when set to an American timezone because of DST
console.log(format('2021-03-09T10:00:00Z', 'E HH mm', { in: tz('America/New_York') }));
console.log(format('2021-04-06T09:00:00Z', 'E HH mm', { in: tz('America/New_York') }));
console.log(format('2021-03-09T10:00:00Z', 'E HH mm', { in: tz('America/Los_Angeles') }));
console.log(format('2021-04-06T09:00:00Z', 'E HH mm', { in: tz('America/Los_Angeles') }));

Actual:

// Is just outputting the values in UTC
Tue 10 00
Tue 09 00
Tue 10 00
Tue 09 00

Expected:

Tue 05 00
Tue 05 00
Tue 02 00
Tue 02 00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants