moment-business-days
Advanced tools
Comparing version 0.0.2 to 0.0.3
78
index.js
@@ -65,4 +65,80 @@ 'use strict'; | ||
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() { | ||
var me = this.clone(); | ||
var day = 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() { | ||
var me = this.clone(); | ||
var day = 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) { | ||
done = true; | ||
}; | ||
}; | ||
return weeksArr; | ||
}; | ||
moment.fn.monthNaturalWeeks = function() { | ||
var me = this.clone(); | ||
var day = 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) { | ||
done = true; | ||
}; | ||
}; | ||
return weeksArr; | ||
}; | ||
module.exports = moment; |
{ | ||
"name": "moment-business-days", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "MomentJS pluin to use business days ", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "./test.js" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
@@ -60,2 +60,62 @@ # moment-business-days | ||
**monthBusinessDays()** | ||
Retrieve an array of the business days in the month, each one is a moment object. | ||
```javascript | ||
//Busines days in month January 2015 | ||
moment('01-01-2015', 'DD-MM-YYYY').monthBusinessDays() | ||
/* | ||
[ { _isAMomentObject: true, | ||
_i: '01-01-2015', | ||
_f: 'DD-MM-YYYY', | ||
_isUTC: false, | ||
_pf:{ ... }, | ||
_locale: { ... }, | ||
_d: Thu Jan 01 2015 00:00:00 GMT-0600 (CST) | ||
} { | ||
... | ||
}, | ||
( ... ) | ||
] | ||
*/ | ||
``` | ||
**monthNaturalDays()** | ||
Is like monthBusinessDays(), but this method will include the weekends on it's response. | ||
**monthBusinessWeeks()** | ||
Retrieve an array of arrays, these arrays are the representation of a business weeks and each week (array) have it own business days (Monday to Friday). There could be the case that one week (array) have less than 5 days, this is because the month started on the middle of the week, for example: the first week of January 2015 just have two days, Thursday 1st and Friday 2nd. **Each day in the week arrays are moment objects.** | ||
```javascript | ||
//Busines weeks in month January 2015 | ||
moment('01-01-2015', 'DD-MM-YYYY').monthBusinessWeeks() | ||
/* | ||
[ [ { _isAMomentObject: true, | ||
_i: '01-01-2015', | ||
_f: 'DD-MM-YYYY', | ||
_isUTC: false, | ||
_pf: [...], | ||
_locale: [...], | ||
_d: Thu Jan 01 2015 00:00:00 GMT-0600 (CST) | ||
}, { _isAMomentObject: true, | ||
_i: '01-01-2015', | ||
_f: 'DD-MM-YYYY', | ||
_isUTC: false, | ||
_pf: [...], | ||
_locale: [...], | ||
_d: Fri Jan 02 2015 00:00:00 GMT-0600 (CST) } | ||
], | ||
[...] | ||
] | ||
*/ | ||
``` | ||
**monthNaturalWeeks()** | ||
It's like monthBusinessWeeks(), but this method will include weekends on it's response. | ||
The objects returned by functions are momentjs objects (**except isBusinessDay**) so you can handle it with moment native functions. |
12
test.js
@@ -21,1 +21,13 @@ 'use strict'; | ||
console.log(moment('02-02-2015', 'DD-MM-YYYY').nextBusinessDay()._d); | ||
console.log('Busines days in this month: ') | ||
console.log(moment('01-01-2015', 'DD-MM-YYYY').monthBusinessDays()); | ||
console.log('Busines weeks in this month: ') | ||
console.log(moment('01-01-2015', 'DD-MM-YYYY').monthBusinessWeeks()); | ||
console.log('Natural days in this month: ') | ||
console.log(moment('01-01-2015', 'DD-MM-YYYY').monthNaturalDays()); | ||
console.log('Natural weeks in this month: ') | ||
console.log(moment('01-01-2015', 'DD-MM-YYYY').monthNaturalWeeks()); |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
10445
148
1
121