Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for moment-timezone #22

Open
billforward-alex opened this issue Feb 9, 2017 · 8 comments
Open

support for moment-timezone #22

billforward-alex opened this issue Feb 9, 2017 · 8 comments

Comments

@billforward-alex
Copy link

billforward-alex commented Feb 9, 2017

This library is hardcoded to require('moment'); — this makes it hard to coexist with other plugins.

Better would be to expose an interface which lets the consumer supply a reference to moment.

Sorry for not making a pull request, but here's what I did on my side (surrounded with a module.exports = function(moment) {};)…

'use strict';

/**
https://github.com/kalmecak/moment-business-days
original by GitHub user `kalmecak`, commit 8d96fce3bfbd9ea900243df2cb18fbda207dbe54

The MIT License (MIT)

Copyright (c) 2015 christian

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

module.exports = function(moment) {
    moment.fn.isHoliday = function () {
        var locale = this.localeData();

        if (locale._holidays) {
            if (locale._holidays.indexOf(this.format(locale._holidayFormat)) >= 0) return true;
        }

        return false;
    };

    moment.fn.isBusinessDay = function() {
        if (this.day() === 0 || this.day() === 6) return false;
        if (this.isHoliday()) return false;
        return true;
    };

    moment.fn.businessDaysIntoMonth = function () {
        var businessDay = this.isBusinessDay() ? this : this.prevBusinessDay();
        var monthBusinessDays = businessDay.monthBusinessDays();
        var businessDaysIntoMonth;
        monthBusinessDays.map(function (day, index) {
            if (day.format('M/DD/YY') === businessDay.format('M/DD/YY'))
                businessDaysIntoMonth = index + 1;
        });

        return businessDaysIntoMonth;
    };

    moment.fn.businessDiff = function(param) {
        param = moment(param);
        var signal = param.unix() < this.unix()?1:-1;
        var start = moment.min(param, this).clone();
        var end = moment.max(param, this).clone();
        var start_offset = start.day() - 7;
        var end_offset = end.day();

        var end_sunday = end.clone().subtract('d', end_offset);
        var start_sunday = start.clone().subtract('d', start_offset);
        var weeks = end_sunday.diff(start_sunday, 'days') / 7;

        start_offset = Math.abs(start_offset);
        if (start_offset == 7) {
          start_offset = 5;
        } else if (start_offset == 1) {
          start_offset = 0;
        } else {
          start_offset -= 2;
        };

        if (end_offset == 6) {
          end_offset--;
        };

        return signal * (weeks * 5 + start_offset + end_offset);
    };

    moment.fn.businessAdd = function(days) {
        var signal = days < 0 ? -1 : 1;
        var daysRemaining = Math.abs(days);
        var d = this.clone();
        while (daysRemaining) {
          d.add(signal, 'd');
          if (d.isBusinessDay()) {
            daysRemaining--;
          };
        };
        return d;
    };

    moment.fn.businessSubtract = function(days) {
        return this.businessAdd(-days);
    };


    moment.fn.nextBusinessDay = function() {
        var loop = 1;
        var limit = 7;
        while (loop < limit) {
            if (this.add(1, 'd').isBusinessDay()) {
                break;
            };
            loop++;
        };
        return this;
    };

    moment.fn.prevBusinessDay = function() {
        var loop = 1;
        var limit = 7;
        while (loop < limit) {
            if (this.subtract(1, 'd').isBusinessDay()) {
                break;
            };
            loop++;
        };
        return this;
    };

    moment.fn.monthBusinessDays = function() {
        var me = this.clone();
        var day = me.clone().startOf('month');
        var end = me.clone().endOf('month');
        var daysArr = [];
        var done = false;
        while (!done) {
            if (day.isBusinessDay()) {
                daysArr.push(day.clone());
            };
            if(end.diff(day.add(1,'d')) < 0) {
                done = true;
            };
        };
        return daysArr;
    };

    moment.fn.monthNaturalDays = function(fromToday) {
        var me = this.clone();
        var day = fromToday ? me.clone() : me.clone().startOf('month');
        var end = me.clone().endOf('month');
        var daysArr = [];
        var done = false;
        while (!done) {
            daysArr.push(day.clone());
            if(end.diff(day.add(1,'d')) < 0) {
                done = true;
            };
        };
        return daysArr;
    };

    moment.fn.monthBusinessWeeks = function(fromToday) {
        var me = this.clone();
        var day = fromToday ? me.clone() : me.clone().startOf('month');
        var end = me.clone().endOf('month');
        var weeksArr = [];
        var daysArr = [];
        var done = false;

        while(!done) {
            if(day.day() >= 1 && day.day() < 6) {
                daysArr.push(day.clone());
            };
            if(day.day() === 5) {
                weeksArr.push(daysArr);
                daysArr = [];
            };
            if(end.diff(day.add(1,'d')) < 0) {
                if(daysArr.length < 5) {
                    weeksArr.push(daysArr);
                };
                done = true;
            };
        };
        return weeksArr;
    };

    moment.fn.monthNaturalWeeks = function(fromToday) {
        var me = this.clone();
        var day = fromToday ? me.clone() : me.clone().startOf('month');
        var end = me.clone().endOf('month');
        var weeksArr = [];
        var daysArr = [];
        var done = false;

        while(!done) {
            daysArr.push(day.clone());
            if(day.day() === 6) {
                weeksArr.push(daysArr);
                daysArr = [];
            };
            if(end.diff(day.add(1,'d')) < 0) {
                if(daysArr.length < 7) {
                    weeksArr.push(daysArr);
                };
                done = true;
            };
        };
        return weeksArr;
    };
    
    return moment;
};
@billforward-alex
Copy link
Author

You can invoke like so:

var momentTz = require('moment-timezone');
var moment = require('./moment-business-days')(momentTz);

@nekdolan
Copy link

To me it seems this lib works with moment-timezone out of the box.

@nbkhope
Copy link

nbkhope commented Oct 25, 2018

It would be nice if moment allowed for some plugin functionality that allows you to add more features instead of having separate libraries requiring moment.

@rognales
Copy link

I +1 this!

@rognales
Copy link

To me it seems this lib works with moment-timezone out of the box.

How did you make it work?

@zeckdude
Copy link

Any examples on how to make this work with ES6 import syntax?

@khasburrahman
Copy link

Does it count business day correctly when using moment-timezone?
I mean the start of saturday and sunday are different in each timezone right in UTC time?

@carelulu-igor
Copy link

Awesome job @billforward-alex, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants