Skip to content

Instantly share code, notes, and snippets.

@lac5
Created June 25, 2019 22:40
Show Gist options
  • Save lac5/bdb4f1bd3b012e4fded6502dae8b66d1 to your computer and use it in GitHub Desktop.
Save lac5/bdb4f1bd3b012e4fded6502dae8b66d1 to your computer and use it in GitHub Desktop.
export function getWeek() {
let date = this;
let thisThu = new Date(date.valueOf() + ((7 - date.getDay()) % 7 - 3) * 86400000);
let firstDay = new Date(thisThu.getFullYear(), 0, 1);
return Math.ceil((thisThu.valueOf() - firstDay.valueOf() + 86400000) / 604800000);
}
export function getUTCWeek() {
let date = this;
let thisThu = new Date(date.valueOf() + ((7 - date.getUTCDay()) % 7 - 3) * 86400000);
let firstDay = new Date(thisThu.getUTCFullYear(), 0, 1);
return Math.ceil((thisThu.valueOf() - firstDay.valueOf() + 86400000) / 604800000);
}
export function getQuarter() {
return Math.floor(this.getMonth() / 4) + 1;
}
export function getUTCQuarter() {
return Math.floor(this.getUTCMonth() / 4) + 1;
}
export function getQuarterDay() {
return Math.floor((this.valueOf() - new Date(this.getFullYear(), (getQuarter.call(this) - 1) * 4 + 1, 0).valueOf()) / 86400000);
}
export function getUTCQuarterDay() {
return Math.floor((this.valueOf() - new Date(this.getUTCFullYear(), (getUTCQuarter.call(this) - 1) * 4 + 1, 0).valueOf()) / 86400000);
}
export function getYearDay() {
return Math.floor((this.valueOf() - new Date(this.getFullYear(), 0, 0).valueOf()) / 86400000);
}
export function getUTCYearDay() {
return Math.floor((this.valueOf() - new Date(this.getUTCFullYear(), 0, 0).valueOf()) / 86400000);
}
export default function() {
Date.prototype.getWeek = getWeek;
Date.prototype.getUTCWeek = getUTCWeek;
Date.prototype.getQuarter = getQuarter;
Date.prototype.getUTCQuarter = getUTCQuarter;
Date.prototype.getQuarterDay = getQuarterDay;
Date.prototype.getUTCQuarterDay = getUTCQuarterDay;
Date.prototype.getYearDay = getYearDay;
Date.prototype.getUTCYearDay = getUTCYearDay;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment