@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
31 lines (30 loc) • 1.46 kB
JavaScript
import { adjustZonedBusinessDays, isValidAmount } from "../../internal/index.js";
import { isValidZonedDateTime } from "../validate/index.js";
/**
* Return a zoned ISO 8601 datetime string with `amount` business days added to `value`.
*
* - Uses fixed ISO Monday–Friday business days (no locale awareness).
* - Saturday and Sunday are skipped during the count.
* - Preserves the original time component through the operation.
* - Returns "" on invalid input.
*
* @param value ISO 8601 zoned datetime string
* @param amount number of business days to add
* @returns Zoned ISO 8601 datetime string after adding business days, or "" on invalid input
*
* @example addZonedBusinessDays("2024-03-15T14:30:00-04:00[America/New_York]", 1) // "2024-03-18T14:30:00-04:00[America/New_York]"
* @example addZonedBusinessDays("2024-03-16T14:30:00-04:00[America/New_York]", 1) // "2024-03-18T14:30:00-04:00[America/New_York]"
* @example addZonedBusinessDays("2024-03-15T14:30:00-04:00[America/New_York]", 0) // "2024-03-15T14:30:00-04:00[America/New_York]"
* @example addZonedBusinessDays("invalid", 1) // ""
*/
export function addZonedBusinessDays(value, amount) {
if (!isValidZonedDateTime(value) || !isValidAmount(amount)) {
return "";
}
if (amount === 0) {
return value;
}
const absAmount = Math.abs(amount);
const direction = amount > 0 ? 1 : -1;
return adjustZonedBusinessDays(value, direction, absAmount);
}