npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

datetime-utc-elements

v0.0.5

Published

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

Downloads

62

Readme

datetime-utc-elements

UPDATE: version 0.0.4 now uses a form associated custom element

UPDATED UPDATE: version 0.0.5 now includes for when you want to separate date and time

You will need to use this polyfill if your browser does not support the ElementInternals API. 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 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 as well:

  • datetime - string in ISO format
  • format - string specifying format. Uses the format specified in date-fns here.

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

import 'datetime-utc-elements';