Whenever an application is made in various languages, formatting of dates is one of the important aspects; however moment.js is a fantastic time & date library with lots of great features and utilities.
If a developer
is working on a performance sensitive web application, it might cause a
performance issue due to its size
or the way it is structured, due to which the developers look for various
alternatives..
Some of the alternatives to moment is given
below-
Date-fns
is a one of the popular time manipulation library for Javascript, chosen by
many developers to replace moment.js .
Date-fns provides over 130 functions to manipulate dates in the browser and
Node.js. Date-fns is built using pure functions and keeps things immutable
while not changing passed date instances. It works well with bundlers such as
webpack, Browserify, or Rollup and also supports tree-shaking.
This library contains around sixty different
locales. To use one or more locales, you need to import them like this:
import {
es,
enCA,
it,
ptBR } from 'date-fns/locale'
- format, which returns the
formatted date, taking as parameters the date, a string representing the
pattern to format the date and an object with options like the locale and
the index of the first day of the week
- formatDistance, which
returns the distance between the given dates in words, taking as
parameters the dates to compare and an object with options like the locale
or whether to include seconds
- formatDistanceToNow is
the same as formatDistance but only takes one date.
- formatDistanceStrict is
the same as formatDistance but without using helpers like almost, over, or less than. The options object has properties to force a time unit and to
specify the way to round partial units
- formatRelative, which
represents the date in words relative to a given base date. It can also
take an options object as an argument, to set the locale and the
index of the first day of the week
import {
format,
formatDistance,
formatDistanceToNow,
formatDistanceStrict,
formatRelative,
addDays
}
from "date-fns";
import { es, enCA, ro, it, ptBR } from
"date-fns/locale";
//
Output: septiembre / 19
console.log(format(new Date(), "MMMM '/'
yy", { locale: es }));
//
Output: in less than 10 seconds
console.log(
formatDistance(
new Date(2019, 8, 1, 0, 0, 15),
new Date(2019, 8, 1, 0, 0, 10),
{ locale: enCA, includeSeconds: true, addSuffix: true }
)
);
//
Output: less than 10 seconds ago
console.log(
formatDistance(
new Date(2019, 8, 1, 0, 0, 10),
new Date(2019, 8, 1, 0, 0, 15),
{ locale: enCA, includeSeconds: true, addSuffix: true }
)
);
//
Output: circa 15 ore (assuming now is 9/20/2019 15:00)
console.log(formatDistanceToNow(new Date(2019,
8, 20), { locale: ro }));
//
Output: 0 minuti
console.log(
formatDistanceStrict(
new Date(2019, 8, 1, 0, 0, 15),
new Date(2019, 8, 1, 0, 0, 10),
{ locale: it, unit: "minute" }
)
);
//
Output: un minuto
console.log(
formatDistanceStrict(
new Date(2019, 8, 1, 0, 0, 10),
new Date(2019, 8, 1, 0, 0, 15),
{ locale: it, unit: "minute", roundingMethod: "ceil"
}
)
);
//
Output: amanhã às 14:48
console.log(formatRelative(addDays(new Date(),
1), new Date(), { locale: ptBR }));
Luxon-
Built by one of Moment’s
maintainers, and at 6.5K stars, luxon is a Javascript library for working with
dates and times with a more comprehensive interface than the native types it
wraps. Unlike moment, Luxon objects are immutable so making changes actually
means creating a new instance with different properties.
he first thing we want to see is the
DateTime as a string. Luxon returns ISO 8601 strings:
DateTime.local().toString() //=> '2017-09-14T03:20:34.091-04:00'
Getting
at components
We can get at the components of the
time individually through getters. For example:
dt = DateTime.local()
dt.year //=> 2017
dt.month //=> 9
dt.day //=> 14
dt.second //=> 47
dt.weekday //=> 4
At nearly 12K stars
day.js is a 2KB date library alternative to Moment.js with a similar API. This
library also helps you parse, validate, manipulate, and display dates and
times, and is also immutable and chainable. Instead of modifying the native Date.prototype
, Day.js
creates a wrapper for the Date object, called Dayjs
object. All API operations
that change the Dayjs
object in some way will
return a new instance of it.
By default, Day.js comes with the
United States English locale. To use other locales, you need to import them
like this:
import 'dayjs/locale/pt';
import localeDe from 'dayjs/locale/de'; //
With a custom alias for the locale object
dayjs.locale('pt') // use Portuguese
locale globally
// To use the locale just in certain
places
console.log(
dayjs()
.locale(localeDe)
.format()
);
console.log( dayjs('2018-4-28', { locale:
'pt' }) );
Ms.js-
Brought to us by Zeit,
ms.js is a narrow yet surprisingly useful library that converts various time
formats into milliseconds. It also works the other way around, converting
milliseconds into human-readable time formats. ms works in both Node.js and the
browser. Some of the features of Ms.js is
- Works both in Node.js and in the browser
- If a number is supplied to
ms
, a string with a unit is returned - If a string that contains the number is
supplied, it returns it as a number (e.g.: it returns
100
for'100'
) - If you pass a string
with a number and a valid unit, the number of equivalent milliseconds is
returned
Time
Format Written-Out
ms(60000, { long: true }) // "1 minute"
ms(2 * 60000, { long: true }) // "2 minutes"
ms(-3 * 60000, { long: true }) // "-3 minutes"
ms(ms('10 hours'), { long: true }) // "10 hours"
Timeago.js-
Timeago is a ~2KB library
that performs a specific useful action: converting time formats to present how
much time has passed since then. 0 dependencies, and the library also supports
localization
Instadate-
At “only” 800 stars,
Instadate is a small library with a handful of performance orianted features.
Instadate is more of a wrapper around the native JavaScript Date than a full on
date library. It can run thousands of time and date manipulations per second,
and also works with ES5 and ES6. The library has a handful of dependancies,
including lodash.difference.
JS-Joda-
At “only”
900 stars, JS-Joda is an immutable date and time library for javascript with a
domain-driven API based on the ISO8601 calendar. It’s uniqueness lies in the
fact that it doesn’t wrap Javascript’s Date object. Instead, , js-joda is a
standalone date and time implementation. The library has no external
dependancies.
For more such topics visit www.skillbakery.com
No comments:
Post a Comment