A lightweight, locale aware formatter for strings containing unicode date tokens.

Overview

Date Token Format

A lightweight (~2kB), locale aware formatter for strings containing unicode date tokens.

Test coverage Minimised code size Types included License: ISC

Usage

Install the package using:

yarn add date-token-format

Import it to your project:

import { formatToken } from "date-token-format"

Then the formatToken method can be used to format a date to produce a string.

formatToken(date: Date, format: string, locale?: string)

The date object should be a valid JavaScript date. The format string should contain one or more formats, and the optional locale string is a ISO 639-1-compatible locale code such as en-US or fr.

Example usage: formatToken

const date = new Date('2021-08-27T12:34:56')
formatToken(date, 'EEEE', 'en-US')
//=> Friday

formatToken(date, 'EEEE', 'de')
//=> Freitag

Auto-detecting browser locale:

const locale = window.navigator.userLanguage || window.navigator.language
const date = new Date('2021-08-27T12:34:56')
formatToken(date, 'EEEE', locale)
//=> weekday in the browser's locale

Formats

The following options, based on unicode date field symbols can be used to generate a locale-based formatted string.

token description example output
yyyy Full year 2021
yy Short year 21
MMMM Full month (text) August
MMM Short month (text) Aug
MM 2-digit month 08
M Numeric month 8
dd 2-digit day 07
d Numeric day 7
EEEE Full weekday Friday
EEE Short weekday Fri
EE Narrow weekday F
HH 24-hour hour 08
H 24-hour hour 08
h 12-hour hour 8 AM
mm Minutes (2-digits) 03
m Minutes 3
ss Seconds (2-digits) 06
s Seconds 6
SSS Fractional seconds (3) 789
SS Fractional seconds (2) 78
S Fractional seconds (1) 7
a AM / PM AM
HH:mm Hours and minutes (24-hour) 03:06
HH:mm:ss Hours/minutes/seconds (24-hour) 03:06:07
h:mm Hours and minutes (12-hour) 3:06 AM
h:mm:ss Hours/minutes/seconds (12-hour) 3:06:07 AM

Browser support

This utility uses the toLocaleString method to provide locale support. This means all locales are supported via the browser without the need for any extra locale configuration.

It is widely supported across all modern and many not-so modern browsers.

To ensure better browser support, options such as dateStyle and timeStyle are avoided in favour of more specific options.

Limitations

As this utility makes use of toLocaleString, it is not able to support as many options as you might find in libraries such as date-fns, such as international support for ordinals.

Be aware of potential clashing token letters. For example, if you use s in your format, it will be converted into seconds. This version does not yet include a method for excluding parts of format strings from being processed.

ISC License

Copyright 2021 Donovan Hutchinson

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

You might also like...

CalendarPickerJS - A minimalistic and modern date-picker component/library 🗓️👨🏽‍💻 Written in Vanilla JS

CalendarPickerJS The simple and pretty way to let a user select a day! Supports all major browser. Entirely written in Vanilla JavaScript with no depe

Dec 6, 2022

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API

English | 简体中文 | 日本語 | Português Brasileiro | 한국어 | Español (España) | Русский Fast 2kB alternative to Moment.js with the same modern API Day.js is a

Dec 28, 2022

Nepali Date Picker jQuery Plugin 🇳🇵

Nepali Date Picker Nepali Date Picker jQuery Plugin for everyone. 🇳🇵 Installation npm install nepali-date-picker Demo and Documentation https://leap

Sep 29, 2022

React Native Week Month Date Picker

React Native Week Month Date Picker

React Native Week Month Date Picker Date picker with a week and month view Installation npm install react-native-week-month-date-picker Dependencies T

Dec 27, 2022

Easy to get a date.

Easy to get a date.

date2data 테이블에 날짜별 데이터 넣을 때마다 새로 객체 만들어서 작업하기가 매우 귀찮아서 만들었다. moment.js를 쓰기에는 구현하고자 하는 내용이 너무 가벼웠음 Install npm i date2data Usage import {getMonthlyDate

Apr 12, 2022

This library provide a fast and easy way to work with date.

This library provide a fast and easy way to work with date.

Calendar.js Calendar.js provide a fast way to work with dates when you don't wanna deal with hours, minute, second and so on. It means that Calendar.j

Apr 27, 2022

A tiny and fast zero-dependency date-picker built with vanilla Javascript and CSS.

A tiny and fast zero-dependency date-picker built with vanilla Javascript and CSS.

A tiny zero-dependency and framework-agnostic date picker that is incredibly easy to use! Compatible with any web UI framework, vanilla JS projects, and even HTML-only projects!

Jan 22, 2021

lightweight, powerful javascript datetimepicker with no dependencies

lightweight, powerful javascript datetimepicker with no dependencies

flatpickr - javascript datetime picker Motivation Almost every large SPA or project involves date and time input. Browser's native implementations of

Jan 3, 2023

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS

A refreshing JavaScript Datepicker — lightweight, no dependencies, modular CSS

Pikaday A refreshing JavaScript Datepicker Lightweight (less than 5kb minified and gzipped) No dependencies (but plays well with Moment.js) Modular CS

Jan 4, 2023
Owner
Donovan Hutchinson
Donovan Hutchinson
Create Persian Calendar as html helper with tag builder c# , and convert selected persian date to gregorian date

Persian-Calendar Use JS,Html,CSS,C# White theme for Persian Calendar , easy to use. Create Persian Calendar as html helper. Use Tag builder in c# for

Tareq Awwad 4 Feb 28, 2022
A date picker web component, and spiritual successor to duet date picker

<date-picker> A date picker web component, based on duet date picker, except authored with Lit and using shadow DOM. This is a work in progress. Todo:

Nick Williams 25 Aug 3, 2022
Lightweight and simple JS date formatting and parsing

fecha Lightweight date formatting and parsing (~2KB). Meant to replace parsing and formatting functionality of moment.js. NPM npm install fecha --save

Taylor Hakes 2k Jan 5, 2023
⏰ Day.js 2KB immutable date-time library alternative to Moment.js with the same modern API

English | 简体中文 | 日本語 | Português Brasileiro | 한국어 | Español (España) | Русский Fast 2kB alternative to Moment.js with the same modern API Day.js is a

null 41.7k Dec 28, 2022
⏳ Modern JavaScript date utility library ⌛️

date-fns provides the most comprehensive, yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js. ?? Documentation

date-fns 30.6k Dec 29, 2022
Reusable date picker component for React

React DayPicker DayPicker is a reusable date picker component for React. $ npm install react-day-picker@next Beta version ⚠️ This branch is for the ne

Giampaolo Bellavite 4.8k Dec 28, 2022
🕑 js-joda is an immutable date and time library for JavaScript.

js-joda is an immutable date and time library for JavaScript. It provides a simple, domain-driven and clean API based on the ISO8601 calendar.

null 1.5k Dec 27, 2022
DEPRECATED: Timezone-enabled JavaScript Date object. Uses Olson zoneinfo files for timezone data.

TimezoneJS.Date A timezone-enabled, drop-in replacement for the stock JavaScript Date. The timezoneJS.Date object is API-compatible with JS Date, with

Matthew Eernisse 830 Nov 20, 2022
Date() for humans

date Date is an english language date parser for node.js and the browser. For examples and demos, see: http://matthewmueller.github.io/date/ Update: d

Matthew Mueller 1.5k Jan 4, 2023
:clock8: :hourglass: timeago.js is a tiny(2.0 kb) library used to format date with `*** time ago` statement.

timeago.js timeago.js is a nano library(less than 2 kb) used to format datetime with *** time ago statement. eg: '3 hours ago'. i18n supported. Time a

hustcc 4.9k Jan 4, 2023