You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
exportfunctioncombineDateAndTime({
date,
time,}: {date: TZDate;time: string;}): TZDate{// Validate the input date.if(Number.isNaN(date.getTime())){thrownewTypeError('Invalid date format');}// Parse the time string.consttimeMatch=/^(\d{1,2}):(\d{2})$/.exec(time);if(!timeMatch){thrownewTypeError('Invalid time format');}// Validate hours and minutes.consthours=Number.parseInt(timeMatch[1],10);constminutes=Number.parseInt(timeMatch[2],10);if(hours<0||hours>23||minutes<0||minutes>59){thrownewTypeError('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!));constresult=setMilliseconds(setMinutes(setHours(date,hours,{in: tz(date.timeZone!)}),minutes,{in: tz(date.timeZone!),}),0,{in: tz(date.timeZone!)},);console.log('result',result);returnresult;}
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 DSTconsole.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
I'm trying to set the time of a date in a specific timezone:
Basically I want a function, that given a
TZDate
and a time inHH:mm
, sets that time on the date, but preserves the date in the timezone.Here is the code I wrote:
Actual
Expected
The text was updated successfully, but these errors were encountered: