# datetime-utc-elements

> UTC on the server. Local time on the browser. Time is relative

Latest version **0.0.6** (published 2025-09-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install datetime-utc-elements
pnpm add datetime-utc-elements
yarn add datetime-utc-elements
bun add datetime-utc-elements
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2025-09-27 |
| First published | 2021-03-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 92 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Chris Nelson |
| Maintainers | superchris |

## Links

- npm: https://www.npmjs.com/package/datetime-utc-elements
- npm.io page: https://npm.io/package/datetime-utc-elements

## Dependencies (4)

- [date-fns](https://npm.io/package/date-fns.md) ^2.19.0
- [lit-html](https://npm.io/package/lit-html.md) ^1.3.0
- [date-fns-tz](https://npm.io/package/date-fns-tz.md) ^1.1.3
- [lit-element](https://npm.io/package/lit-element.md) ^2.4.0

## Recent versions

- 0.0.6 (latest) — 2025-09-27
- 0.0.5 — 2021-07-26
- 0.0.4 — 2021-04-05
- 0.0.3 — 2021-04-02
- 0.0.2 — 2021-03-22
- 0.0.1 — 2021-03-22

## README

## datetime-utc-elements

### UPDATE: version 0.0.4 now uses a form associated custom element
### UPDATED UPDATE: version 0.0.5 now includes <date-and-time-utc-input> for when you want to separate date and time
### UPDATE: version 0.0.6 adds default format MM/dd/yyyy for <datetime-utc-output>

You will need to use [this polyfill](https://www.npmjs.com/package/element-internals-polyfill) if your browser does not support the [ElementInternals API](https://html.spec.whatwg.org/multipage/custom-elements.html#element-internals). If this is not to your liking, use version 0.0.2 which uses a hidden input instead.

### Description

1. Store dates and times as UTC.
2. Display them in the user's time zone
3. Profit!

The idea here is hopefully pretty simple. We store our dates in UTC, and use custom elements to display dates and times to the user in their browser's time zone. This means that the server never sends or receives anything other than UTC. We leverage the `Intl.DateTimeFormat` api to get the user's time zone, and the excellent `date-time-fns` library to convert to UTC when we send and receive it. Finally, we use the built in date time editing of our browser by using an `<input type="datetime-local">`. 

Two custom elements work together to implement our solution:

### `<datetime-utc-input>`

This is a custom element which renders a datetime-local input and uses the [ElementInternals API](https://html.spec.whatwg.org/multipage/custom-elements.html#element-internals) to set the form value with the date as UTC. It takes 2 attributes:

* name - used to set form value
* value - an initial value, if desired, in ISO format

### `<date-and-time-utc-input>`

The same as `<datetime-utc-input>` but renders as separate date and time inputs. Both will display and expect input in local timezone. Same attributes as `<datetime-utc-input>`. 

### `<datetime-utc-output>`

This element is responsible for taking a UTC datetime and displaying it in the browser timezone. It takes 2 attributes:

* datetime - string in ISO format
* format - string specifying format (optional, defaults to `MM/dd/yyyy`). Uses the format specified in `date-fns` [here](https://date-fns.org/v2.19.0/docs/format).

## Styling

Both input elements expose shadow parts to let you style the internal input(s) in the spoooky Shadow DOM.

`<datetime-utc-input>` gives you an `input` part.
`<date-and-time-utc-input>` gives you `date-input` and `time-input` parts.

### Usage

```
npm install datetime-utc-elements
```

In your javascript

```javascript
import 'datetime-utc-elements';
```

---
_Source: https://npm.io/package/datetime-utc-elements · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
