# formatX12TimeElement

Industry: Intermodal

## Signature

```ts
formatX12TimeElement(value: string, form: X12TimeElementForm): string
```

Write an ISO 8601 time as an X12 data element 337 (Time) value, in the form the caller names by the element's own mask. `parseX12Time` reads the result back when it is given no qualifier.

- Element 337 is the time element of many X12 segments: in freight, `AT7-06`, `G62-04` and `DTM-03`. No format qualifier travels with it, so nothing in the segment says which of its four forms a value is in. The definition is release 005010, read from [Stedi's X12-licensed dictionary](https://www.stedi.com/edi/x12-005010/element/337); the X12 text itself was not reached. ### Forms

  | Form | Digits | The dictionary's definition |
  |---|---|---|
  | `HHMM` | 4 | "H = hours (00-23), M = minutes (00-59)" |
  | `HHMMSS` | 6 | "S = integer seconds (00-59)" |
  | `HHMMSSD` | 7 | "D = tenths (0-9)" |
  | `HHMMSSDD` | 8 | "DD = hundredths (00-99)" |

- `value` is a time, as `isValidTime` accepts it: `HH:MM`, with seconds and a fraction of a second if it has them. A date-time, a date or a time with an offset returns `""`.
- **The form alone fixes the digits written.** It is never read off the value, so the width of the field does not depend on the data: a time with no fraction is `14300000` under `HHMMSSDD`, and a time with one is `1430` under `HHMM`.
- Precision is cut to the mask, never rounded. `D` is the whole tenths of the second and `DD` its whole hundredths, so `14:30:45.999` is `1430459` under `HHMMSSD` and `14304599` under `HHMMSSDD`. A written value never names a later second than its input.
- **The form is a mask, not a data element 1250 code.** `TM` and `TS` return `""` here. To write a time that travels with a 1250 qualifier (`DTP-03`, `DTM-06`), use `formatX12Time`.
- Returns `""` for any other form and for a non-string argument.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `value` | `string` | ISO 8601 time (e.g. "14:30:00.12") |
| `form` | `X12TimeElementForm` | The element 337 mask to write: `HHMM`, `HHMMSS`, `HHMMSSD` or `HHMMSSDD` |

## Returns

the X12 time value, or "" on invalid input

## Types

### X12TimeElementForm

The four forms of X12 data element 337 (Time), each named by the element's own mask. Release 005010, read from [Stedi's X12-licensed dictionary](https://www.stedi.com/edi/x12-005010/element/337); the X12 text itself was not reached. The dictionary's definition: "Time expressed in 24-hour clock time as follows: HHMM, or HHMMSS, or HHMMSSD, or HHMMSSDD, where H = hours (00-23), M = minutes (00-59), S = integer seconds (00-59) and DD = decimal seconds; decimal seconds are expressed as follows: D = tenths (0-9) and DD = hundredths (00-99)".

```ts
import type { X12TimeElementForm } from "@northguild/gmt";
```

```ts
"HHMM" | "HHMMSS" | "HHMMSSD" | "HHMMSSDD"
```

## Examples

```ts
import { formatX12TimeElement } from "@northguild/gmt/intermodal/format";

formatX12TimeElement("14:30:00.12", "HHMMSSDD") // "14300012"
```

## Source

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