2.0.3 • Published 5 months ago

kk-date v2.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

kk-date

kk-date is a fastest JavaScript library that parses, validations, manipulates, and displays dates and times. If you use Moment.js or Day.js already you can easily use kk-date.

  • 🕒 Familiar Moment.js API & Day.js
  • 💪 Immutable
  • 🔥 Chainable
  • 🚀 Native Date
  • 🏁 Performance
  • ✅ All browsers supported

Install

npm install kk-date

Performance

if the same function is called 400k times.

date: '23:50:55' expected format: YYYY-MM-DD HH:mm:ss
Packagems
kk-date125msWIN!
moment933.953ms
day.js1267ms
date: '2024-09-17 23:50:55' expected format: HH:mm:ss
Packagems
kk-date123msWIN!
day.js588.194ms
moment1760ms
diff 2024-01-01 - 2024-01-30
Packagems
kk-date126msWIN!
day.js313.93ms
moment1806ms

API

It's easy to use kk-date to parse, validate, manipulate, and display dates and times.

REQUIRE:

  const kk_date = require('kk-date');
Enable Caching (optional);
  kk_date.caching({ status: true, isMemoryStatsEnabled: false, defaultTtl: 1300 });

Gain 300% more speed by enabling cache. default is false

Disable Caching;
  kk_date.caching({ status: false });

Examples:

format:

new kk_date('2024-01-01 23:00:00').format('YYYY-MM-DD')
// 2024-01-01

format:

new kk_date('2024-01-01 23:00:00').format('HH:mm:ss')
// 23:00:00

format:

new kk_date('2024-01-01 23:00:00').format('YYYY-MM-DD HH:mm:ss')
// 2024-01-01 23:00:00

add:

new kk_date('2024-01-01').add(1, 'days').format('YYYY-MM-DD')
// 2024-01-02

add (subtract):

new kk_date('2024-01-05').add(-1, 'days').format('YYYY-MM-DD')
// 2024-01-04

set (day):

new kk_date('2024-01-05').set(1, 'day').format('YYYY-MM-DD')
// 2024-01-01

set (month):

new kk_date('2024-01-05').set(3, 'month').format('YYYY-MM-DD')
// 2024-03-05

diff:

new kk_date('2024-07-19').diff('2024-09-19', 'months')
// 2

diff_range:

new kk_date('2024-07-19').diff_range('2024-09-19', 'months')
// [ '2024-07-19', '2024-08-18', '2024-09-18' ]

isBefore:

new kk_date('2024-09-19').isBefore('2024-09-20')
// true

isAfter:

new kk_date('2024-09-19').isAfter('2024-09-20')
// false

isSameOrAfter:

new kk_date('2024-09-19 18:00:00').isSameOrAfter('2024-09-19 18:01:00')
// false

isSame:

new kk_date('2024-09-19 18:00:00').isSame('2024-09-19 18:00:00')
// true

isBetween:

new kk_date('2024-09-19 18:00:00').isBetween('2024-09-19 12:00:00', '2024-09-19 19:00:00')
// true

Others:

.toDateString();

.toJSON();

.toUTCString();

.toLocaleDateString(locales, options);

.toLocaleString(locales, options);

.toLocaleTimeString(locales, options);

.toTimeString();

.valueOf();

Notes:

The kk-date package is designed with simplicity and performance in mind, offering a streamlined way to handle date objects in JavaScript. When you instantiate the class, you can effortlessly access the native JavaScript Date object via the date key, making it intuitive for those familiar with the standard Date API.

Key Features

  • Simplicity at its Core: The package is written to be as straightforward as possible, minimizing overhead and ensuring ease of use for basic date handling.
  • Direct Access to Native Date: Seamlessly work with the native Date object, giving you the full power of JavaScript's built-in date functionalities.
  • Optimized for Performance: If speed and efficiency are critical for your application, kk-date is the perfect fit, delivering performance without sacrificing usability.
  • Ongoing Development: While kk-date is currently tailored for basic date operations, we are actively developing it to support more complex date manipulations and formats in future releases.

Whether you're building a lightweight application or need a reliable solution for high-performance date handling, kk-date is the package you’ve been looking for.

.format() supported templates:

YYYY-MM-DDTHH:mm:ss
YYYY-MM-DD HH:mm:ss
YYYY-MM-DD HH:mm
YYYY-MM-DD HH
YYYY-MM-DD
YYYYMMDD
DD.MM.YYYY
YYYY.MM.DD
YYYY.MM.DD HH:mm
YYYY.MM.DD HH
YYYY.MM.DD HH:mm:ss
DD.MM.YYYY HH:mm:ss
DD.MM.YYYY HH:mm
DD-MM-YYYY
DD-MM-YYYY HH:mm
DD-MM-YYYY HH:mm:ss
DD-MM-YYYY HH
dddd
HH:mm:ss
HH:mm
x (Unix timestamps in milliseconds),
X (Unix timestamps in seconds)

.add() supported types:

seconds
minutes
hours
days
months
years

Performance Tests (cache enabled):

Test NoinputOutputkk-dateMomentDay.jsdate-fns
Test 0new Date2024-09-04 15:30:00163.529ms374.127msN/A799.957ms
Test 123:50:552024-09-04 23:50:55125.225ms954.643ms1.202s807.904ms
Test 223:5023:50173.597ms591.453ms796.459ms331.762ms
Test 323:50:552024.09.04 23:50:55123.571ms952.301ms1.183s798.924ms
Test 42024-09-17 23:50:5523:50:55123.55ms1.635s569.191msN/A
Test 52024-09-17 23:50:552024-09-17119.81ms1.620s571.845msN/A
Test 62024-09-17 23:50:552024-09-17 23:50:55248.857ms1.769s721.491msN/A
Test 72024-09-17 23:50:5517.09.2024 23:50:55158.367ms1.787s740.896msN/A
Test 82024-09-17 23:50:552024.09.17 23:50:55165.413ms1.789s738.167msN/A
Test 923:50:55 isValidBoolean64.785ms671.449msN/AN/A
Test 10172399667727.08.2024 05:51:17173.895ms416.96ms797.691msN/A
Test 111984307700027.08.2056 05:55:00172.668ms414.394ms757.555msN/A
Test 121984307700027.08.2056 05:55:00169.85ms416.481ms753.213msN/A
Test 13diff (days) 2024-01-01, 2024-01-3029126.738ms1.662s312.043msN/A
Test 14isBefore 2024-01-01, 2024-01-30true126.651ms1.617s345.618msN/A
Test 15isBetween 2024-01-01, 2024-01-30true185.48ms2.357s717.21msN/A
Test 16isAfter 2024-01-01, 2024-01-30true122.112ms1.565s335.345msN/A
Test 17isSame 2024-01-01true123.669ms1.576s456.979msN/A
1.1.4

7 months ago

1.1.3

7 months ago

2.0.3

5 months ago

2.0.2

5 months ago

2.0.1

5 months ago

2.0.0

6 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.1.2

9 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.15

10 months ago

1.0.14

10 months ago

1.0.13

10 months ago

1.0.12

10 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago