Coder Social home page Coder Social logo

aparkerdavid / datetime-utc-elements Goto Github PK

View Code? Open in Web Editor NEW

This project forked from launchscout/datetime-utc-elements

0.0 1.0 0.0 311 KB

Time is relative. But it should always be in UTC on the server.

License: MIT License

JavaScript 100.00%

datetime-utc-elements's Introduction

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';

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.