Skip to content

Instantly share code, notes, and snippets.

@MrRhodes
Created December 16, 2014 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrRhodes/72181ea03bf1ed94f37a to your computer and use it in GitHub Desktop.
Save MrRhodes/72181ea03bf1ed94f37a to your computer and use it in GitHub Desktop.
Trying to write a simple next/last day thingy with Moment.
var moment = require('moment');
function Datey() {
this.date = moment();
this.modifier = 1;
return this;
}
Datey.prototype.last = function() {
this.modifier = -1;
return this;
}
Datey.prototype.next = function() {
this.modifier = 1;
return this;
}
Datey.prototype.sunday = function() {
return this.offset(7);
}
Datey.prototype.monday = function() {
return this.offset(1);
}
Datey.prototype.tuesday = function() {
return this.offset(2);
}
Datey.prototype.wednesday = function() {
return this.offset(3);
}
Datey.prototype.thursday = function() {
return this.offset(4);
}
Datey.prototype.friday = function() {
return this.offset(5);
}
Datey.prototype.saturday = function() {
return this.offset(6);
}
Datey.prototype.offset = function(d) {
var day = this.date.isoWeekday();
if (d >= day && this.modifier < 0) d -= 7;
if (d <= day && this.modifier > 0) d += 7;
return this.date.clone().isoWeekday(d).format('ddd Do');
}
var d = new Datey();
console.log('last tue',d.last().tuesday());
console.log('last wed',d.last().wednesday());
console.log('last thu',d.last().thursday());
console.log('last fri',d.last().friday());
console.log('last sat',d.last().saturday()); // -1
console.log('last sun',d.last().sunday()); // 0
console.log('last mon',d.last().monday()); // 1
console.log(moment().format('ddd Do'),'<< Now');
console.log('next wed',d.next().wednesday());
console.log('next thu',d.next().thursday());
console.log('next fri',d.next().friday());
console.log('next sat',d.next().saturday());
console.log('next sun',d.next().sunday());
console.log('next mon',d.next().monday());
console.log('next tue',d.next().tuesday());
/*
s m t w t f s
weekdays 0 1 2 3 4 5 6
last 0 1 * -4 -3 -2 -1
next
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment