Git Product home page Git Product logo

datecollator's Introduction

DateCollator

Date collation by specified date parts for granular comparison.

Description

DateCollator is like Intl.Collator, except it allows for the comparison of Dates by (most of) the date part types specified by Intl.DateTimeFormat.prototype.formatToParts(). The configuration options are similar to some of Intl.Collator's options:

dateSensitivity: The granularity and order of date parts to use with each comparison, default ["year", "month", "day", "hour", "minute", "second", "fractionalSecond"]. Possible values are an array of:

  • "era"
  • "year"
  • "month"
  • "weekday"
  • "day"
  • "dayPeriod"
  • "hour"
  • "minute"
  • "second"
  • "fractionalSecond"

dateUsage: Whether the comparison is to be performed using local or UTC values, default "local". Possible values are:

  • "local" Use Date.prototype functions like getMinutes().
  • "utc" Use Date.prototype functions like getUTCMinutes().

Importing

import DateCollator from 'datecollator';

β€”ORβ€”

import {DateCollator} from 'datecollator';

Examples

Check whether Dates occur during the same day.

const dateCollatorDownToDay = new DateCollator( null, {dateSensitivity:['year', 'month', 'day']});
dateCollatorDownToDay.compare( new Date( 2020, 2, 23, 9, 0 ), new Date( 2020, 2, 23, 17, 0 ));
// returns 0

Check whether Dates occur during the time (hour and minute) of any day.

const dateCollatorJustByTime = new DateCollator( null, {dateSensitivity:['hour', 'minute'], dateUsage:'utc'});
dateCollatorJustByTime.compare( new Date( 2020, 2, 23, 11, 0 ), new Date( 2026, 2, 23, 11, 0 ));
// returns 0

Check whether Dates occur during the same "dayPeriod" (meridian) of any day.

const dateCollatorDownToDayPeriod = new DateCollator( null, {dateSensitivity:['dayPeriod']});
dateCollatorDownToDayPeriod.compare( new Date( 2020, 2, 23, 9, 0 ), new Date( 2026, 2, 23, 11, 0 ));
// returns 0

Check whether Dates occur during the same "weekday" of any week.

const dateCollatorJustByWeekday = new DateCollator( null, {dateSensitivity:['weekday'], dateUsage:'utc'});
dateCollatorJustByWeekday.compare( new Date( 2020, 2, 23, 9, 0 ), new Date( 2026, 2, 23, 11, 0 ));
// returns 0

Default collator for sorting.

const dateCollator = new DateCollator();
let dates =
[
	new Date( 2020, 2, 23, 17, 0 ),
	new Date( 2020, 2, 23, 11, 0 ),
	new Date( 2020, 2, 23, 9, 0 ),
	new Date( 2026, 2, 23, 11, 0 )
];
dates.sort( dateCollator.compare ).map(( date ) => date.toISOString());
// returns
// [
//	"2020-03-23T14:00:00.000Z",
//	"2020-03-23T16:00:00.000Z",
//	"2020-03-23T22:00:00.000Z",
//	"2026-03-23T16:00:00.000Z"
// ]

datecollator's People

Contributors

dependabot[bot] avatar markmyoung avatar

Watchers

 avatar  avatar

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.