Skip to content

Commit 41d7fe7

Browse files
committed
getOffset
1 parent 4c9cd42 commit 41d7fe7

1 file changed

Lines changed: 12 additions & 22 deletions

File tree

  • backend/firebase/functions/src/utilities
Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
1-
export function utcOffset(isoString: string, timezone = 'America/New_York') {
1+
export function utcOffset(isoString: string, timeZone = 'America/new_york') {
22
console.log('conversion:input', isoString);
3-
const originalDate = new Date(
4-
new Date(isoString).toLocaleString('en-US', {
5-
timeZone: timezone,
6-
})
7-
);
8-
const utcDate = new Date(
9-
new Date(isoString).toLocaleString('en-US', {
10-
timeZone: 'UTC',
11-
})
12-
);
13-
14-
// // Milliseconds from original Date
15-
const offset = originalDate.getTime() - utcDate.getTime();
16-
const output = new Date(Date.parse(originalDate.toISOString()) + offset)
3+
const isoStringDate = new Date(isoString);
4+
const offset = getOffset(timeZone, isoStringDate);
5+
console.log('conversion:offsethours', offset / 60 / 60 / 1000);
6+
const output = new Date(Date.parse(isoStringDate.toISOString()) + offset)
177
.toISOString()
188
.replace('Z', '');
199
console.log('conversion:output', output);
2010
return output;
2111
}
2212

13+
const getOffset = (timeZone = 'UTC', date = new Date()) => {
14+
const utcDate = new Date(date.toLocaleString('en-US', { timeZone: 'UTC' }));
15+
const tzDate = new Date(date.toLocaleString('en-US', { timeZone }));
16+
return tzDate.getTime() - utcDate.getTime();
17+
};
18+
2319
const calendlyDate = '2022-01-26T14:00:00.000000Z';
2420
console.log('hardcoded:calendlyDate', calendlyDate);
2521
console.log('hardcoded:fromFunc', utcOffset(calendlyDate));
26-
console.log('hardcoded:shouldMatch', '2022-01-26T09:00:00.000000-05:00');
27-
console.log(
28-
'hardcoded:shouldMatchLocale',
29-
new Date('2022-01-26T09:00:00.000000-05:00').toLocaleString('en-US', {
30-
timeZone: 'America/New_York',
31-
})
32-
);
22+
console.log('hardcoded:shouldMatch', '2022-01-26T09:00:00.000');

0 commit comments

Comments
 (0)