Coder Social home page Coder Social logo

moment-business's Introduction

moment-business

Travis build status Code Climate Test Coverage Dependency Status devDependency Status

Utilities for working with week days and weekend days in Moment. It assumes a Western workweek, in which weekends are Saturday and Sunday.

Looking for a vanilla JS version of this library? Check out bizniz.js

Motivation

Moment is an indispensable tool for working with dates in Javascript, but it doesn't (and won't) supply methods for working with week days or weekend days.

This library supplies you with those missing tools.

Why this library?

There are alternative libraries for these methods, but this one uses constant-time algorithms, not loops. Loops are easier for a human to write, but are much slower for a computer to resolve.

Getting Started

Install this library through npm.

npm install moment-business

Next, import it into your project.

import business from 'moment-business';

business.isWeekDay(someMoment);

Note: this library is also available through Bower.

Guides

Dates and times

This library only works when you pass in dates without times. What this means is that you should not use Moment objects built from dates such as "2016-07-25T13:43:00.000+00:00". If you have datetimes that you wish to use with this library, then you must:

  1. use Moment's startOf() method to set this to be the start of the day
  2. manually strip out the information after the T before creating your Moment object

If you do not do this, then this library will not return the results that you expect.

Intervals

This library uses inclusive start, exclusive end intervals. What this means is that the start day is included in the result, but the end day is not.

This is consistent with how Moment's intervals work.

For example, the total number of days between March 1st, 2020 and March 2nd, 2020 will be computed as 1.

API

weekDays( startMoment, endMoment )

Calculate the number of week days between startMoment and endMoment. Week days are Monday through Friday.

If endMoment comes before startMoment, then this function will return a negative value.

weekendDays( startMoment, endMoment )

Calculate the number of weekend days between the moment and otherMoment. Weekend days are Saturday and Sunday.

If endMoment comes before startMoment, then this function will return a negative value.

addWeekDays( moment, amount )

Add week days to a moment, modifying the original moment. Returns the moment.

subtractWeekDays( moment, amount )

Subtract week days from the moment, modifying the original moment. Returns the moment.

isWeekDay( moment )

Whether or not the Moment is a week day (Monday - Friday).

isWeekendDay( moment )

Whether or not the Moment occurs on Saturday or Sunday.

moment-business's People

Contributors

jamesplease avatar megawac avatar leobetosouza avatar

Watchers

Michael Breedlove avatar James Cloos 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.