Skip to main content
Version: v0.0.7

Function: dateShift()

dateShift(value, ctx): string | null

Shift a date by the context's deterministic per-patient offset, preserving intervals. Supports ISO YYYY-MM-DD, HL7 YYYYMMDD, and an ISO datetime (YYYY-MM-DDThh:mm…). Fails closed (null) on an unparseable or invalid date.

Timezone-independent. Only the calendar-date portion is shifted (via UTC calendar math); a datetime's time-of-day and zone designator are preserved verbatim, so the same input yields the same output on every host regardless of the machine's TZ. Because the offset is a whole number of days and the clock/zone are untouched, intervals are preserved exactly.

The offset is not returned - only the shifted value is. Two dates for the same patient move by the same amount, so the number of days between them is unchanged.

Parameters

value

string

The date value to shift.

ctx

DeidContext

The de-identification context (must carry a patientId scope for the offset).

Returns

string | null

The shifted date at the original precision, or null if it could not be parsed.

Throws

DeidError with code DEID_NO_KEY if the context has no per-patient scope.

Example

import { dateShift, createDeidContext } from "@cosyte/deid";

const ctx = createDeidContext({ key: "secret", patientId: "patient-1" });
const a = dateShift("2020-01-01", ctx);
const b = dateShift("2020-01-11", ctx);
// a and b are shifted, but remain exactly 10 days apart.