// The locale "en-US" is used here because it allows the
// formatter to produce numeric values that are parseable by JS
- // Date objects / components.
+ // Date objects and other date/time components.
const formatter = Intl.DateTimeFormat('en-US', formatOps);
const parts = formatter.formatToParts();
const response: DateParts = new DateParts();
return response;
}
+
+ // Returns a date object equivalent to the provided date parts
+ // and time zone. The resulting date will be a standard JS Date
+ // object, expressed in the local time zone.
+ //
+ // For example, assuming the browser time zone is America/New_York,
+ // this would turn parts for "America/Los_Angeles"
+ // {year: 2019, month: 08, day: 24, hour: 12, minute: 00}
+ // into 2019-08-24 15:00:00-0400, i.e. the local date equivalent.
+ static datePartsToDate(parts: DateParts, timeZone: string): Date {
+
+ // Create a date from the parts in the local time zone
+ const localDate = new Date(
+ parts.year, parts.month, parts.day, parts.hour, parts.minute);
+
+ // Shift the date by transforming it into the requested time zone
+ const offsetParts = DateUtil.dateToParts(localDate, timeZone);
+
+ // Turn the shifted parts into another local date.
+ const offsetDate = new Date(offsetParts.year, offsetParts.month,
+ offsetParts.day, offsetParts.hour, offsetParts.minute);
+
+ // The difference between the two is the difference that must
+ // be applied to dates in the local time zone to represent
+ // the requetsed date parts from the requested time zone.
+ const offset = localDate.getTime() - offsetDate.getTime();
+
+ localDate.setTime(localDate.getTime() + offset);
+
+ return localDate;
+ }
}