Skip to content

Instantly share code, notes, and snippets.

@SparK-Cruz
Last active July 10, 2025 01:21
Show Gist options
  • Save SparK-Cruz/0bcbf2e0a9252199d3ca103f4afc3f73 to your computer and use it in GitHub Desktop.
Save SparK-Cruz/0bcbf2e0a9252199d3ca103f4afc3f73 to your computer and use it in GitHub Desktop.
C13Date Object
const monthNames = [
'Capricornus',
'Aquarius',
'Pisces',
'Aries',
'Taurus',
'Gemini',
'Cancer',
'Leo',
'Virgo',
'Libra',
'Scorpius',
'Sagittarius',
'Serpentarius',
];
const dayNames = [
'Solis',
'Lunae',
'Martis',
'Mercurii',
'Iovis',
'Veneris',
'Saturni',
'Terra',
];
const SECOND = 1000;
const MINUTE = SECOND * 60;
const HOUR = MINUTE * 60;
const DAY = HOUR * 24;
const MONTH = DAY * 28;
const C13Date = (() => {
function pad(number, length) {
const current = number.toString();
if (current.length >= length)
return current;
return ('0'.repeat(length) + current).substr(-length);
}
function cls(time) {
if (typeof time === 'undefined')
time = Date.now();
if (!['string', 'object', 'number'].includes(typeof time)
|| !time instanceof Date)
throw 'Invalid arguement error for C13Date constructor.';
const isIsoDate = typeof time === 'string' && time.length === 10 && time.split('-').length === 3;
const def = new Date();
const _time = new Date(isIsoDate ? def : (time === '' ? def : time));
this.setTime(_time.getTime());
if (isIsoDate) {
const broken = time.split('-');
const year = parseInt(broken[0]);
const month = parseInt(broken[1]) - 1;
const date = parseInt(broken[2]);
if ((month == 12 && date <= 30) || (month < 12 && date <= 28)) {
this.setFullYear(year);
this.setMonth(month);
this.setDate(date);
}
}
}
const ins = cls.prototype;
// static
cls.now = function () {
Date.now.apply(this, arguments);
};
cls.parse = function () {
Date.parse.apply(this, arguments);
};
cls.UTC = function () {
Date.UTC.apply(this, arguments);
};
// instance
ins.getDate = function () {
const base = this._internal - this._start;
const month = base / MONTH | 0;
const date = (base % MONTH) / DAY | 0;
if (month < 13)
return date + 1;
return date + 29;
};
ins.getDay = function () {
const date = this.getDate() - 1;
if (date > 27)
return 7;
return date % 7;
};
ins.getFullYear = function () {
return (new Date(this._internal)).getUTCFullYear();
};
ins.getHours = function () {
const base = this._internal - this._start;
return (base % DAY) / HOUR | 0;
};
ins.getMilliseconds = function () {
const base = this._internal - this._start;
return base % SECOND;
};
ins.getMinutes = function () {
const base = this._internal - this._start;
return (base % HOUR) / MINUTE | 0;
};
ins.getMonth = function () {
const base = this._internal - this._start;
return Math.min((base / MONTH | 0), 12);
};
ins.getSeconds = function () {
const base = this._internal - this._start;
return (base % MINUTE) / SECOND | 0;
};
ins.getTime = function () {
return this._internal;
};
ins.getTimezoneOffset = function () {
return 0;
};
ins.getUTCDate = ins.getDate;
ins.getUTCDay = ins.getDay;
ins.getUTCFullYear = ins.getFullYear;
ins.getUTCHours = ins.getHours;
ins.getUTCMilliseconds = ins.getMilliseconds;
ins.getUTCMinutes = ins.getMinutes;
ins.getUTCMonth = ins.getMonth;
ins.getUTCSeconds = ins.getSeconds;
ins.getYear = function () {
return (new Date(this._internal)).getYear();
};
ins.isLeapYear = function () {
const n = this.getFullYear();
return n % 400 === 0 || (n % 100 !== 0 && n % 4 === 0);
};
ins.setDate = function (date) {
const offset = (this.getDate() - 1) * DAY;
this._internal += ((date - 1) * DAY) - offset;
this.setTime(this._internal);
};
ins.setFullYear = function (year) {
const base = this._internal - this._start;
this._start = Date.parse(year.toString());
this._internal = this._start + base;
};
ins.setHours = function (hours) {
const offset = this.getHours() * HOUR;
this._internal += (hours * HOUR) - offset;
this.setTime(this._internal);
};
ins.setMilliseconds = function (milliseconds) {
const offset = this.getMilliseconds();
this._internal += milliseconds - offset;
this.setTime(this._internal);
};
ins.setMinutes = function (minutes) {
const offset = this.getMinutes() * MINUTE;
this._internal += (minutes * MINUTE) - offset;
this.setTime(this._internal);
};
ins.setMonth = function (month) {
const offset = this.getMonth() * MONTH;
this._internal += (month * MONTH) - offset;
this.setTime(this._internal);
};
ins.setSeconds = function (seconds) {
const offset = this.getSeconds() * SECOND;
this._internal += (seconds * SECOND) - offset;
this.setTime(this._internal);
};
ins.setTime = function (time) {
this._internal = time;
this._start = Date.parse((new Date(this._internal)).getUTCFullYear().toString());
};
ins.setUTCDate = ins.setDate;
ins.setUTCDay = ins.setDay;
ins.setUTCFullYear = ins.setFullYear;
ins.setUTCHours = ins.setHours;
ins.setUTCMilliseconds = ins.setMilliseconds;
ins.setUTCMinutes = ins.setMinutes;
ins.setUTCMonth = ins.setMonth;
ins.setUTCSeconds = ins.setSeconds;
ins.setYear = function (year) {
const dummy = new Date(0);
dummy.setYear(year);
this.setFullYear(dummy.getUTCFullYear());
}
ins.toDateString = function () {
return [
this.getFullYear(),
monthNames[this.getMonth()],
this.getDate() + ',',
dayNames[this.getDay()],
].join(' ');
};
ins.toISOString = function () {
return [
pad(this.getFullYear(), 4),
'-',
pad(this.getMonth() + 1, 2),
'-',
pad(this.getDate(), 2),
'T',
pad(this.getHours(), 2),
':',
pad(this.getMinutes(), 2),
':',
pad(this.getSeconds(), 2),
'.',
pad(this.getMilliseconds(), 3),
'Z'
].join('');
};
ins.toJSON = ins.toISOString;
ins.toGMTString = function () {
return [
this.toDateString(),
this.toLocaleTimeString(),
'UTC',
].join(' ');
};
ins.toLocaleDateString = ins.toDateString;
ins.toLocaleString = ins.toGMTString;
ins.toLocaleTimeString = function () {
return [
pad(this.getHours(), 2),
pad(this.getMinutes(), 2),
pad(this.getSeconds(), 2),
].join(':');
};
ins.toString = ins.toGMTString;
ins.toTimeString = ins.toLocaleTimeString;
ins.toUTCString = ins.toGMTString;
ins.valueOf = function () {
return this._internal;
}
cls.monthNames = monthNames;
cls.dayNames = dayNames;
return cls;
})();
if (typeof window !== 'undefined') {
window.C13Date = C13Date;
}
if (typeof module !== 'undefined') {
module.exports = C13Date;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment