@szegedsw/lib-node
Version:
A little framework published by Szeged Software Zrt. in order to enhance api endpoint security and create reuseable code. Email module, Logging system, and much more. Further improvements are expected.
82 lines • 3.11 kB
JavaScript
;
/**
* @param picture use YYYY or YY for get year. use MM or M to get month, use DD or D to get date.
* use h or hh or use h24 or hh24 to get hours.
* use m or mm to get minutes.
* use s or ss to get seconds.
* use i to get millis.
* All values are returned in UTC.
*/
// eslint-disable-next-line func-names
Date.prototype.toFormattedString = function (picture) {
let val = picture;
// tslint:disable: no-invalid-this
const year = this.getUTCFullYear();
const month = this.getUTCMonth() + 1;
const day = this.getUTCDate();
const hours = this.getUTCHours();
const minutes = this.getUTCMinutes();
const seconds = this.getUTCSeconds();
const millis = this.getUTCMilliseconds();
// tslint:disable: max-line-length no-magic-numbers
if (val.search("YYYY") > -1) {
val = val.replace("YYYY", year.toString());
}
else if (val.search("YY") > -1) {
val = val.replace("YY", year.toString().substr(2, 2));
}
if (val.search("MM") > -1) {
val = val.replace("MM", month > 9 ? month.toString() : `0${month.toString()}`);
}
else if (val.search("M") > -1) {
val = val.replace("M", month.toString());
}
if (val.search("DD") > -1) {
val = val.replace("DD", day > 9 ? day.toString() : `0${day.toString()}`);
}
else if (val.search("D") > -1) {
val = val.replace("D", day.toString());
}
if (val.search("hh24") > -1) {
val = val.replace("hh24", hours > 9 ? hours.toString() : `0${hours.toString()}`);
}
else if (val.search("h24") > -1) {
val = val.replace("h24", hours.toString());
}
if (val.search("hh") > -1) {
// eslint-disable-next-line no-nested-ternary
val = val.replace("hh", hours > 9 ? (hours > 11 ? (hours - 12).toString() : hours.toString()) : `0${hours.toString()}`);
}
else if (val.search("h") > -1) {
val = val.replace("h", hours > 11 ? (hours - 12).toString() : hours.toString());
}
if (val.search("mm") > -1) {
val = val.replace("mm", minutes > 9 ? minutes.toString() : `0${minutes.toString()}`);
}
else if (val.search("m") > -1) {
val = val.replace("m", minutes.toString());
}
if (val.search("ss") > -1) {
val = val.replace("ss", seconds > 9 ? seconds.toString() : `0${seconds.toString()}`);
}
else if (val.search("s") > -1) {
val = val.replace("s", seconds.toString());
}
if (val.search("i") > -1) {
val = val.replace("i", millis > 9 ? millis.toString() : `0${millis.toString()}`);
}
return val;
};
// eslint-disable-next-line func-names
Date.prototype.toUTC = function () {
return new Date(this.valueOf() + this.getTimezoneOffset() * 60 * 1000);
};
// eslint-disable-next-line func-names
Date.prototype.toLocal = function () {
return new Date(this.valueOf() - this.getTimezoneOffset() * 60 * 1000);
};
// eslint-disable-next-line func-names
Date.prototype.substractDays = function (days) {
return new Date(this.valueOf() - days * 24 * 60 * 60 * 1000);
};
//# sourceMappingURL=date.ext.js.map