Skip to content

Instantly share code, notes, and snippets.

@choegyumin
Last active April 27, 2021 11:07
Show Gist options
  • Save choegyumin/2862de1931cf48f858de86ab5dfe37b9 to your computer and use it in GitHub Desktop.
Save choegyumin/2862de1931cf48f858de86ab5dfe37b9 to your computer and use it in GitHub Desktop.
Extending feature (add, format) for Date object in Vanilla Javascript (:see_no_evil: Monkey Patch)
(function () {
'use strict';
var MONTHS_NAME = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var DAYS_NAME = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; // Weekdays
var DEFAULT_FORMAT = 'YYYY-MM-DDTHH:mm:ssZ';
var FORMAT_REGEX = /\[.*?\]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g;
var STRIP_BRACKETS_REGEX = /\[|\]/g;
Date.prototype.addDate = function (value) { // Day
this.setDate(this.getDate() + value);
return this;
};
Date.prototype.addFullYear = function (value) {
this.setFullYear(this.getFullYear() + value);
return this;
};
Date.prototype.addHours = function (value) {
this.setHours(this.getHours() + value);
return this;
};
Date.prototype.addMilliseconds = function (value) {
this.setMilliseconds(this.getMilliseconds() + value);
return this;
};
Date.prototype.addMinutes = function (value) {
this.setMinutes(this.getMinutes() + value);
return this;
};
Date.prototype.addMonth = function (value) {
this.setMonth(this.getMonth() + value);
return this;
};
Date.prototype.addSeconds = function (value) {
this.setSeconds(this.getSeconds() + value);
return this;
};
Date.prototype.addTime = function (value) {
this.setTime(this.getTime() + value);
return this;
};
Date.prototype.addYear = function (value) {
this.setYear(this.getYear() + value);
return this;
};
Date.prototype.getMonthName = function () {
return MONTHS_NAME[this.getMonth()];
};
Date.prototype.getDayName = function () { // Weekday
return DAYS_NAME[this.getDay()];
};
// https://github.com/iamkun/dayjs
Date.prototype.toFormatString = function (format, options) {
format = typeof format === 'string' ? format : DEFAULT_FORMAT;
options = typeof options === 'object' ? options : {};
options.utc = typeof options.utc === 'boolean' ? options.utc : false;
var digitPadStart = function (value, length) {
length = typeof length === 'number' ? length : 2;
var str = String(value);
if (!str || str.length >= length) return str;
return Array((length + 1) - str.length).join('0') + str;
};
var year = options.utc ? this.getUTCFullYear() : this.getFullYear(),
month = options.utc ? this.getUTCMonth() : this.getMonth(),
date = options.utc ? this.getUTCDate() : this.getDate(), // Day
day = options.utc ? this.getUTCDay() : this.getDay(), // Weekday
hours = options.utc ? this.getUTCHours() : this.getHours(),
minutes = options.utc ? this.getUTCMinutes() : this.getMinutes(),
seconds = options.utc ? this.getUTCSeconds() : this.getSeconds(),
milliseconds = options.utc ? this.getUTCMilliseconds() : this.getMilliseconds(),
meridiem = hours < 12 ? 'AM' : 'PM',
zone = (function () {
if (options.utc) return '+00:00';
var negMinuts = this.getTimezoneOffset();
var minutes = Math.abs(negMinuts);
return (negMinuts <= 0 ? '+' : '-') +
digitPadStart(Math.floor(minutes / 60), 2) +
':' +
digitPadStart(minutes % 60, 2);
}.bind(this)());
return format.replace(FORMAT_REGEX, function (match) {
if (match.indexOf('[') > -1) return match.replace(STRIP_BRACKETS_REGEX, '');
switch (match) {
case 'YY':
return String(year).slice(-2);
case 'YYYY':
return String(year);
case 'M':
return String(month + 1);
case 'MM':
return digitPadStart(month + 1, 2);
case 'MMM':
return MONTHS_NAME[month].substr(0, 3);
case 'MMMM':
return MONTHS_NAME[month];
case 'D':
return String(date);
case 'DD':
return digitPadStart(date, 2);
case 'd':
return String(day);
case 'dd':
return DAYS_NAME[day].substr(0, 2);
case 'ddd':
return DAYS_NAME[day].substr(0, 3);
case 'dddd':
return DAYS_NAME[day];
case 'H':
return String(hours);
case 'HH':
return digitPadStart(hours, 2);
case 'h':
case 'hh':
if (hours === 0) return 12;
return digitPadStart(hours < 13 ? hours : hours - 12, match === 'hh' ? 2 : 1);
case 'a':
return meridiem.toLowerCase();
case 'A':
return meridiem;
case 'm':
return String(minutes);
case 'mm':
return digitPadStart(minutes, 2);
case 's':
return String(seconds);
case 'ss':
return digitPadStart(seconds, 2);
case 'SSS':
return digitPadStart(milliseconds, 3);
case 'Z':
return zone;
case 'ZZ':
return zone.replace(':', '');
default:
return '';
}
});
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment