Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude weekends between two dates using Moment.js

I am trying to exclude weekends in my JavaScript code. I use moment.js and having difficulty choosing the right variable for 'days'.

So far I have thought that I need to exclude day 6 (saturday) and day 0 (sunday) by changing the weekday variable to count from day 1 to day 5 only. But not sure how it changes.

My jsfiddle is shown here: FIDDLE

HTML:

<div id="myContent">
<input type="radio" value="types" class="syncTypes" name="syncTypes"> <td><label for="xshipping.xshipping1">Free Shipping: (<span id="fsv1" value="5">5</span> to <span id="fsv2" value="10">10</span> working days)</label> </td><br>
    <div id="contacts" style="display:none;border:1px #666 solid;padding:3px;top:15px;position:relative;margin-bottom:25px;">     
    Contacts
</div>
<input type="radio" value="groups" class="syncTypes" name="syncTypes"> <td><label for="xshipping.xshipping2">Express Shipping: (<span id="esv1" value="3">3</span> to <span id="esv2" value="4">4</span> working days)</label> </td>    
<div id="groups" style="display:none;border:1px #666 solid;padding:3px;top:15px;position:relative">     
    Groups
</div>
</div>

JavaScript:

var a = 5; //Free shipping between a
var b = 10;//and b
var c = 3;//Express shipping between c
var d = 4;//and d    
var now = moment();    
var f = "Your item will be delivered between " + now.add("days",a).format("Do MMMM") + " and " + now.add("days",b).format("Do MMMM");
var g = "Your item will be delivered between " + now.add("days".c).format("Do MMMM") + " and " + now.add("days",d).format("Do MMMM");

var h = document.getElementById('contacts');
h.innerHTML = g

var i = document.getElementById('groups');
i.innerHTML = f

$(function() {
    $types = $('.syncTypes');
    $contacts = $('#contacts');
    $groups = $('#groups');
    $types.change(function() {
        $this = $(this).val();
        if ($this == "types") {
            $groups.slideUp(300);
            $contacts.delay(200).slideDown(300);
        }
        else if ($this == "groups") {
            $contacts.slideUp(300);
            $groups.delay(200).slideDown(300);
        }
    });
});
like image 549
tman16 Avatar asked Dec 26 '13 17:12

tman16


People also ask

How do you subtract two dates using a Moment?

You are correct, you can use moment's diff function to subtract two dates (see my example on Plunker): var date1 = moment('2016-10-08 10:29:23'); var date2 = moment('2016-10-08 11:06:55'); var diff = date2. diff(date1);

How do you exclude weekends in power query?

One way of achieving this: Add a calculated column of form IsWeekend = IF(WEEKDAY(Table1[date])>5,1,0) Filter your visual by IsWeekend is 0.


2 Answers

Here you go!

function addWeekdays(date, days) {   date = moment(date); // use a clone   while (days > 0) {     date = date.add(1, 'days');     // decrease "days" only if it's a weekday.     if (date.isoWeekday() !== 6 && date.isoWeekday() !== 7) {       days -= 1;     }   }   return date; } 

You call it like this

var date = addWeekdays(moment(), 5); 

I used .isoWeekday instead of .weekday because it doesn't depend on the locale (.weekday(0) can be either Monday or Sunday).

Don't subtract weekdays, i.e addWeekdays(moment(), -3) otherwise this simple function will loop forever!

Updated JSFiddle http://jsfiddle.net/Xt2e6/39/ (using different momentjs cdn)

like image 174
acoiro Avatar answered Oct 09 '22 02:10

acoiro


Those iteration looped solutions would not fit my needs. They were too slow for large numbers. So I made my own version:

https://github.com/leonardosantos/momentjs-business

Hope you find it useful.

like image 34
lsantos Avatar answered Oct 09 '22 02:10

lsantos