Skip to content

Instantly share code, notes, and snippets.

@benaubin
Last active June 27, 2017 11:08
Show Gist options
  • Save benaubin/ae44799b7e94ce08753a to your computer and use it in GitHub Desktop.
Save benaubin/ae44799b7e94ce08753a to your computer and use it in GitHub Desktop.
Moment Days

Moment DaysOfTheWeek Plus

Some weekday magic by me - Ben

License:

It's under the CC-0.

Install:

Do one of these things

  • Copy & Paste the CoffeeScript or compiled JS into your code.
  • Use the html tag: <script src="https://gist.github.com/penne12/ae44799b7e94ce08753a/raw/moment-daysoftheweek-plus.js"></script>

Use

You can do this now:

moment().firstDayOfMonth() //=> moment().date(1)

or this:

moment().nextDay(1) //=> the next monday
moment().nextDay(5) //=> the next friday

or this:

moment().firstDay(1) //=> the first monday of the month
moment().firstDay(3) //=> the first wednesday of the month

or this:

moment().day(1).allDaysRemainingInMonth() //=> The week's monday, and all the other mondays remaning in the month
moment().day(3).allDaysRemainingInMonth() //=> The week's wednesday, and all the other mondays remaning in the month

or this:

moment().allDays(1) //=> All the mondays in this month
moment().allDays(3) //=> All the wednesdays in this month

so yeah - it's somewhat cool.

moment::firstDayOfMonth = -> @date 1
moment::nextDay = (day) -> @add 1,'d' while @day() != day; @
moment::firstDay = (day) -> @firstDayOfMonth.nextDay day
moment::allDaysRemainingInMonth = ->
t = @clone()
while t.month() == m || m = t.month()
day = t.clone()
t.add 1, 'w'
day
moment::allDays = (day) -> @firstDay(day).allDaysRemainingInMonth()
moment.prototype.firstDayOfMonth = function() {
return this.date(1);
};
moment.prototype.nextDay = function(day) {
while (this.day() !== day) {
this.add(1, 'd');
}
return this;
};
moment.prototype.firstDay = function(day) {
return this.firstDayOfMonth.nextDay(day);
};
moment.prototype.allDaysRemainingInMonth = function() {
var day, m, results, t;
t = this.clone();
results = [];
while (t.month() === m || (m = t.month())) {
day = t.clone();
t.add(1, 'w');
results.push(day);
}
return results;
};
moment.prototype.allDays = function(day) {
return this.firstDay(day).allDaysRemainingInMonth();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment