Skip to content

Instantly share code, notes, and snippets.

@strarsis
Created July 2, 2020 20:42
Show Gist options
  • Save strarsis/9fa3d91e27d1095cd511ff819467e102 to your computer and use it in GitHub Desktop.
Save strarsis/9fa3d91e27d1095cd511ff819467e102 to your computer and use it in GitHub Desktop.
Complied DateTime.ts from Litepicker
var DateTime = /** @class */ (function () {
function DateTime(date, format, lang) {
if (date === void 0) { date = null; }
if (format === void 0) { format = null; }
if (lang === void 0) { lang = 'en-US'; }
if (format) {
this.dateInstance = (DateTime.parseDateTime(date, format, lang));
}
else if (date) {
this.dateInstance = (DateTime.parseDateTime(date));
}
else {
this.dateInstance = (DateTime.parseDateTime(new Date()));
}
this.lang = lang;
}
DateTime.parseDateTime = function (date, format, lang) {
if (format === void 0) { format = 'YYYY-MM-DD'; }
if (lang === void 0) { lang = 'en-US'; }
if (!date)
return new Date(NaN);
if (date instanceof Date)
return new Date(date);
if (date instanceof DateTime)
return date.clone().getDateInstance();
if (/^-?\d{10,}$/.test(date))
return DateTime.getDateZeroTime(new Date(Number(date)));
if (typeof date === 'string') {
var match = format.match(/\[([^\]]+)]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}/g);
if (match) {
var datePattern = {
year: 1,
month: 2,
day: 3,
value: '',
};
var shortMonths = null;
var longMonths = null;
if (match.indexOf('MMM') !== -1) {
shortMonths = this.MONTH_JS.map(function (x) { return new Date(2019, x).toLocaleString(lang, { month: 'short' }); });
}
if (match.indexOf('MMMM') !== -1) {
longMonths = this.MONTH_JS
.map(function (x) { return new Date(2019, x).toLocaleString(lang, { month: 'long' }); });
}
for (var _i = 0, _a = Object.entries(match); _i < _a.length; _i++) {
var _b = _a[_i], k = _b[0], v = _b[1];
var key = Number(k);
var value = String(v);
if (key > 0)
datePattern.value += '.*?'; // any delimiter
switch (value) {
case 'YY':
case 'YYYY':
datePattern.year = key + 1;
datePattern.value += "(\\d{" + value.length + "})";
break;
case 'M':
datePattern.month = key + 1;
datePattern.value += '(\\d{1,2})';
break;
case 'MM':
datePattern.month = key + 1;
datePattern.value += "(\\d{" + value.length + "})";
break;
case 'MMM':
datePattern.month = key + 1;
datePattern.value += "(" + shortMonths.join('|') + ")";
break;
case 'MMMM':
datePattern.month = key + 1;
datePattern.value += "(" + longMonths.join('|') + ")";
break;
case 'D':
datePattern.day = key + 1;
datePattern.value += '(\\d{1,2})';
break;
case 'DD':
datePattern.day = key + 1;
datePattern.value += "(\\d{" + value.length + "})";
break;
}
}
var dateRegex = new RegExp("^" + datePattern.value + "$");
if (dateRegex.test(date)) {
var d = dateRegex.exec(date);
var year = Number(d[datePattern.year]);
var month = Number(d[datePattern.month]) - 1;
if (shortMonths) {
month = shortMonths.indexOf(d[datePattern.month]);
}
else if (longMonths) {
month = longMonths.indexOf(d[datePattern.month]);
}
var day = Number(d[datePattern.day]) || 1;
return new Date(year, month, day, 0, 0, 0, 0);
}
}
}
return DateTime.getDateZeroTime(new Date(date));
};
DateTime.convertArray = function (array, format) {
return array
.map(function (d) {
if (d instanceof Array) {
return d.map(function (d1) { return new DateTime(d1, format); });
}
return new DateTime(d, format);
});
};
DateTime.getDateZeroTime = function (date) {
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0, 0);
};
DateTime.prototype.getDateInstance = function () {
return this.dateInstance;
};
DateTime.prototype.toLocaleString = function (arg0, arg1) {
return this.dateInstance.toLocaleString(arg0, arg1);
};
DateTime.prototype.toDateString = function () {
return this.dateInstance.toDateString();
};
DateTime.prototype.getSeconds = function () {
return this.dateInstance.getSeconds();
};
DateTime.prototype.getDay = function () {
return this.dateInstance.getDay();
};
DateTime.prototype.getTime = function () {
return this.dateInstance.getTime();
};
DateTime.prototype.getDate = function () {
return this.dateInstance.getDate();
};
DateTime.prototype.getMonth = function () {
return this.dateInstance.getMonth();
};
DateTime.prototype.getFullYear = function () {
return this.dateInstance.getFullYear();
};
DateTime.prototype.setMonth = function (arg) {
return this.dateInstance.setMonth(arg);
};
DateTime.prototype.setSeconds = function (arg) {
return this.dateInstance.setSeconds(arg);
};
DateTime.prototype.setDate = function (arg) {
return this.dateInstance.setDate(arg);
};
DateTime.prototype.setFullYear = function (arg) {
return this.dateInstance.setFullYear(arg);
};
DateTime.prototype.getWeek = function (firstDay) {
var target = new Date(this.timestamp());
var dayNr = (this.getDay() + (7 - firstDay)) % 7;
target.setDate(target.getDate() - dayNr);
var startWeekday = target.getTime();
target.setMonth(0, 1);
if (target.getDay() !== firstDay) {
target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
}
return 1 + Math.ceil((startWeekday - target.getTime()) / 604800000);
};
DateTime.prototype.clone = function () {
return new DateTime(this.getDateInstance());
};
DateTime.prototype.isBetween = function (date1, date2, inclusivity) {
if (inclusivity === void 0) { inclusivity = '()'; }
switch (inclusivity) {
default:
case '()':
return this.timestamp() > date1.getTime() && this.timestamp() < date2.getTime();
case '[)':
return this.timestamp() >= date1.getTime() && this.timestamp() < date2.getTime();
case '(]':
return this.timestamp() > date1.getTime() && this.timestamp() <= date2.getTime();
case '[]':
return this.timestamp() >= date1.getTime() && this.timestamp() <= date2.getTime();
}
};
DateTime.prototype.isBefore = function (date, unit) {
if (unit === void 0) { unit = 'seconds'; }
switch (unit) {
case 'second':
case 'seconds':
return date.getTime() > this.getTime();
case 'day':
case 'days':
return new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime()
> new Date(this.getFullYear(), this.getMonth(), this.getDate()).getTime();
case 'month':
case 'months':
return new Date(date.getFullYear(), date.getMonth(), 1).getTime()
> new Date(this.getFullYear(), this.getMonth(), 1).getTime();
case 'year':
case 'years':
return date.getFullYear() > this.getFullYear();
}
throw new Error('isBefore: Invalid unit!');
};
DateTime.prototype.isSameOrBefore = function (date, unit) {
if (unit === void 0) { unit = 'seconds'; }
switch (unit) {
case 'second':
case 'seconds':
return date.getTime() >= this.getTime();
case 'day':
case 'days':
return new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime()
>= new Date(this.getFullYear(), this.getMonth(), this.getDate()).getTime();
case 'month':
case 'months':
return new Date(date.getFullYear(), date.getMonth(), 1).getTime()
>= new Date(this.getFullYear(), this.getMonth(), 1).getTime();
}
throw new Error('isSameOrBefore: Invalid unit!');
};
DateTime.prototype.isAfter = function (date, unit) {
if (unit === void 0) { unit = 'seconds'; }
switch (unit) {
case 'second':
case 'seconds':
return this.getTime() > date.getTime();
case 'day':
case 'days':
return new Date(this.getFullYear(), this.getMonth(), this.getDate()).getTime()
> new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
case 'month':
case 'months':
return new Date(this.getFullYear(), this.getMonth(), 1).getTime()
> new Date(date.getFullYear(), date.getMonth(), 1).getTime();
case 'year':
case 'years':
return this.getFullYear() > date.getFullYear();
}
throw new Error('isAfter: Invalid unit!');
};
DateTime.prototype.isSameOrAfter = function (date, unit) {
if (unit === void 0) { unit = 'seconds'; }
switch (unit) {
case 'second':
case 'seconds':
return this.getTime() >= date.getTime();
case 'day':
case 'days':
return new Date(this.getFullYear(), this.getMonth(), this.getDate()).getTime()
>= new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
case 'month':
case 'months':
return new Date(this.getFullYear(), this.getMonth(), 1).getTime()
>= new Date(date.getFullYear(), date.getMonth(), 1).getTime();
}
throw new Error('isSameOrAfter: Invalid unit!');
};
DateTime.prototype.isSame = function (date, unit) {
if (unit === void 0) { unit = 'seconds'; }
switch (unit) {
case 'second':
case 'seconds':
return this.getTime() === date.getTime();
case 'day':
case 'days':
return new Date(this.getFullYear(), this.getMonth(), this.getDate()).getTime()
=== new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
case 'month':
case 'months':
return new Date(this.getFullYear(), this.getMonth(), 1).getTime()
=== new Date(date.getFullYear(), date.getMonth(), 1).getTime();
}
throw new Error('isSame: Invalid unit!');
};
DateTime.prototype.add = function (duration, unit) {
if (unit === void 0) { unit = 'seconds'; }
switch (unit) {
case 'second':
case 'seconds':
this.setSeconds(this.getSeconds() + duration);
break;
case 'day':
case 'days':
this.setDate(this.getDate() + duration);
break;
case 'month':
case 'months':
this.setMonth(this.getMonth() + duration);
break;
}
return this;
};
DateTime.prototype.subtract = function (duration, unit) {
if (unit === void 0) { unit = 'seconds'; }
switch (unit) {
case 'second':
case 'seconds':
this.setSeconds(this.getSeconds() - duration);
break;
case 'day':
case 'days':
this.setDate(this.getDate() - duration);
break;
case 'month':
case 'months':
this.setMonth(this.getMonth() - duration);
break;
}
return this;
};
DateTime.prototype.diff = function (date, unit) {
if (unit === void 0) { unit = 'seconds'; }
var oneDay = 1000 * 60 * 60 * 24;
switch (unit) {
default:
case 'second':
case 'seconds':
return this.getTime() - date.getTime();
case 'day':
case 'days':
return Math.round((this.timestamp() - date.getTime()) / oneDay);
case 'month':
case 'months':
// @TODO
}
};
DateTime.prototype.format = function (format, lang) {
if (lang === void 0) { lang = 'en-US'; }
var response = '';
var match = format.match(/\[([^\]]+)]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}/g);
if (match) {
var shortMonths = null;
var longMonths = null;
if (match.indexOf('MMM') !== -1) {
shortMonths = DateTime.MONTH_JS
.map(function (x) { return new Date(2019, x).toLocaleString(lang, { month: 'short' }); });
}
if (match.indexOf('MMMM')) {
longMonths = DateTime.MONTH_JS
.map(function (x) { return new Date(2019, x).toLocaleString(lang, { month: 'long' }); });
}
for (var _i = 0, _a = Object.entries(match); _i < _a.length; _i++) {
var _b = _a[_i], k = _b[0], v = _b[1];
var key = Number(k);
var value = String(v);
if (key > 0) {
var prev = match[key - 1];
response += format.substring(format.indexOf(prev) + prev.length, format.indexOf(value));
}
switch (value) {
case 'YY':
response += String(this.getFullYear()).slice(-2);
break;
case 'YYYY':
response += String(this.getFullYear());
break;
case 'M':
response += String(this.getMonth() + 1);
break;
case 'MM':
response += ("0" + (this.getMonth() + 1)).slice(-2);
break;
case 'MMM':
response += shortMonths[this.getMonth()];
break;
case 'MMMM':
response += longMonths[this.getMonth()];
break;
case 'D':
response += String(this.getDate());
break;
case 'DD':
response += ("0" + this.getDate()).slice(-2);
break;
}
}
}
return response;
};
DateTime.prototype.timestamp = function () {
return new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0, 0).getTime();
};
DateTime.MONTH_JS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
return DateTime;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment