# parseX12DateAndTime

Industry: Intermodal

## Signature

```ts
parseX12DateAndTime(date: string, time: string): string
```

Read the date and the time of an X12 segment, sent as two elements, as one ISO 8601 local date-time.

- X12 freight segments carry a moment as separate elements, and this function takes the two as they are transmitted: `AT7-05` and `AT7-06` (in a 214 shipment status), `G62-02` and `G62-04` (a 204 load tender) and `DTM-02` and `DTM-03`. Definitions are release 005010, read from [Stedi's X12-licensed dictionary](https://www.stedi.com/edi/x12-005010/segment/DTM); the X12 text itself was not reached. ### The two elements

  | Element | Name | The dictionary's definition |
  |---|---|---|
  | [373](https://www.stedi.com/edi/x12-005010/element/373) | Date | "Date expressed as CCYYMMDD where CC represents the first two digits of the calendar year" |
  | [337](https://www.stedi.com/edi/x12-005010/element/337) | Time | "Time expressed in 24-hour clock time as follows: HHMM, or HHMMSS, or HHMMSSD, or HHMMSSDD" |

- **The result is local, never UTC.** It has no `Z` and no offset. `AT7` says what a blank time code means: "If AT707 is not present then AT706 represents local time of the status". Reading the value as UTC is the most common EDI timestamp bug.
- **The time code beside them (data element 623) says where the clock was.** For a code that states an offset, resolve the result at that offset: `resolveLocal(parseX12DateAndTime("20240615", "1430"), x12TimeCodeOffset("20"))` is `"2024-06-15T19:30:00Z"`. For a code that names a zone (`x12TimeCodeZone`), pass the result and the IANA zone you map the name to, to `resolveLocal`.
- **Both elements are required.** The function returns one local date-time, which needs both. X12 lets a segment carry one without the other (`G62`: "At least one of G62-01 or G62-03 is required"). A missing or empty date or time returns `""`: read a date alone with `parseX12Date(value, "D8")` and a time alone with `parseX12Time`.
- The time is one of the four element 337 forms, 4, 6, 7 or 8 digits. Seconds are always written, `00` for `HHMM`. Tenths and hundredths become the fraction of the second exactly: `14300012` is `14:30:00.12`. A zero fraction is not written.
- **To write the two elements back**, write the date with `formatX12Date(date, "D8")` and the time with `formatX12TimeElement`, naming the element 337 form by its mask: `formatX12TimeElement("14:30:00.12", "HHMMSSDD")` is `14300012`.
- Fields are checked, not clamped: 29 February 2023, 31 June, hour 24, minute or second 60 and a time of 5 or 9 digits return `""`.
- The date has a four-digit year. Before release 004010, element 373 was `YYMMDD`: read such a date with `parseDateWithPattern("240615", "yyMMdd", undefined, { yearWindow: 2000 })`.
- Returns `""` for a non-string argument.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `date` | `string` | The data element 373 date as transmitted, `CCYYMMDD` (e.g. "20240615") |
| `time` | `string` | The data element 337 time as transmitted (e.g. "1430") |

## Returns

the local date-time as `YYYY-MM-DDTHH:MM:SS`, with a fraction when the time has one, or "" on invalid input

## Examples

```ts
import { parseX12DateAndTime } from "@northguild/gmt/intermodal/parse";

parseX12DateAndTime("20240615", "1430") // "2024-06-15T14:30:00"
```

## Source

[intermodal/parse/parseX12DateAndTime.ts](https://github.com/northguild/gmt/blob/main/packages/gmt/src/intermodal/parse/parseX12DateAndTime.ts)
