Coder Social home page Coder Social logo

moment-business's Introduction

moment-business

Travis build status Test Coverage Dependency 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 leobetosouza avatar megawac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

moment-business's Issues

O(n)!

O(n) => O(1)

  • Add constant time addition
  • Add constant time subtraction

Fix browser export for non-module loader environments

It seems to currently be exporting as window.none. Note that this only affects users who aren't using a module bundler like CJS, AMD, or ES2015 modules. If you are using those module bundlers, then you're good to go.

Add holidays

I know that holidays varies by country, state, city, province, etc

I think would be good have a way to set the holidays, with a array of month/date

For instance with the US federal holidays would be something like this moment.setHolidays(['January 01', 'January 18', 'February 15', 'July 04', 'May 30', 'September 5', 'October 10', 'November 11', 'November 26', 'December 26'])

For NY we would push some more days to the list: moment.addHolidays(['February 12', 'November 08', 'December 25'])

Them when we use methods of moment-bussiness it consider these days not as weekdays, and not as weekendDays.

We can also create methods to count holidays.

What do you think?

Clarify documentation for weekDays / weekendDays

Please can you make the documentation in the README clearer for using weekDays and weekendDays - please make it specific that these have to be used endMoment.weekDays(startMoment)

Just been using this with startMoment.weekDays(endMoment) and puzzling over weird responses - it was only when I looked at the source that I realised what was going on.

Introduce floating point precision.

Often times when calculating week days I'd like more precision to be returned. Currently calculations less then one day get rounded down to zero. I would like to see a flag introduced to signal when to not round down. So we end up with possible values of 0.5 days or 7.3 days.

Undefined is not a function

When I do today.workDays( tomorrow ) where today and tomorrow are moments a day apart, I get Undefined is not a function - is there something I am doing wrong?

.addWorkDays() and .subtractWorkDays() are working fine

Calculation problem if the endDate is weekend

When using the weekDays function,

The result may + 1 day when the startMoment and endMoment are both weekend AND when the endMoment is weekend.

Here are some testing results:

console.log(momentBusiness.weekDays(moment('08-05-2016','DD-MM-YYYY'),moment('15-05-2016','DD-MM-YYYY')));
// Actual Result = 5, Expected Result = 4

console.log(momentBusiness.weekDays(moment('09-05-2016','DD-MM-YYYY'),moment('14-05-2016','DD-MM-YYYY')))
// Actual Result = 5, Expected Result = 4

console.log(momentBusiness.weekDays(moment('07-05-2016','DD-MM-YYYY'),moment('13-05-2016','DD-MM-YYYY')))
// Actual Result = 4, Expected Result = 4 <-- This is correct

weekendDays sometimes incorrect

The following code returns an incorrect value for the number of weekend days:

const start = moment.utc("2016-07-25T13:43:00.000+00:00");
const end = moment.utc("2016-08-22T11:28:00.000+00:00");
business.weekendDays(start, end); // returns 7, expected 8

Is this right?

Move away from moment plugins

@cowboy mentioned that there's not much reason for this to modify the moment obj. directly. I should just expose methods on an obj rather than modifying the moment prototype.

Update the library

It's been a few years since I wrote this, but it's my most popular library by downloads. I should update it!

  1. Update to the latest Node
  2. Fix builds
  3. Modernize the repository

This won't involve any code changes, and no npm releases. It'll simply be a repository refresher.

off-by-one error on weekDays (maybe due to moment#diff)

Seems like there is a problem at moment#diff, used by the weekDays method.

You can reproduce the same bug on moment-business with these tests: https://github.com/jmeas/moment-business/compare/jmeas:master...leobalter:return-proper-weekdays?expand=1

I didn't open a PR with these tests as they're not following the standard from the other tests, we could improve it.

I checked the return from the .diff call at weekDays and the off-by-one is happening there.

How do i use it?

var final = moment(date).weekDays(date2); if like this it isn't working

Using without npm

I tried to use the lib without npm,
by copying the moment-business.js file,
and source it with:
<script src="moment-business.js"></script>

then use it with
import business from 'moment-business';

but I got error below:
Uncaught SyntaxError: Cannot use import statement outside a module

Any idea how to solve this ?

Moment Timezone support

When I try to use this with moment timezone in typescript, it looks like both timezone and business have definition to the Moment but they can't convert to each other.

integrate with moment prototype

would be a lot easier (and cleaner) if you would implement the method through the prototype.

moment().isWeekend() is much nicer then bussiness.isWeekend(moment())

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.