class KdDateFormat { /** * @param dateObj */ constructor(dateObj) { this._date = dateObj || new Date(); } /** * @returns {string} */ getMonthName() { switch (this._date.getMonth()) { case 0: return 'Январь'; case 1: return 'Февраль'; case 2: return 'Март'; case 3: return 'Апрель'; case 4: return 'Май'; case 5: return 'Июнь'; case 6: return 'Июль'; case 7: return 'Август'; case 8: return 'Сентябрь'; case 9: return 'Октябрь'; case 10: return 'Ноябрь'; case 11: return 'Декабрь'; } } /** * @returns {string} */ getMonthShortName() { switch (this._date.getMonth()) { case 0: return 'Янв'; case 1: return 'Фев'; case 2: return 'Мар'; case 3: return 'Апр'; case 4: return 'Май'; case 5: return 'Июн'; case 6: return 'Июл'; case 7: return 'Авг'; case 8: return 'Сен'; case 9: return 'Окт'; case 10: return 'Ноя'; case 11: return 'Дек'; } } /** * @returns {string} */ getDayShortName() { switch (this._date.getDay()) { case 0: return 'Вс'; case 1: return 'Пн'; case 2: return 'Вт'; case 3: return 'Ср'; case 4: return 'Чт'; case 5: return 'Пт'; case 6: return 'Сб'; } } /** * @returns {string} * @description Format: 'Y-m-d' */ toSQLDate() { var yearStr = String(this._date.getFullYear()); var month = this._date.getMonth() + 1; var monthStr = month < 10 ? `0${month}` : String(month); var date = this._date.getDate(); var dateStr = this._date < 10 ? `0${date}` : String(date); return [yearStr, monthStr, dateStr].join('-'); } static getMonthName(monthNumber) { switch (monthNumber) { case 0: return 'Январь'; case 1: return 'Февраль'; case 2: return 'Март'; case 3: return 'Апрель'; case 4: return 'Май'; case 5: return 'Июнь'; case 6: return 'Июль'; case 7: return 'Август'; case 8: return 'Сентябрь'; case 9: return 'Октябрь'; case 10: return 'Ноябрь'; case 11: return 'Декабрь'; } } /** * @param monthNumber * @returns {string} */ static getMonthShortName(monthNumber) { switch (monthNumber) { case 0: return 'Янв'; case 1: return 'Фев'; case 2: return 'Мар'; case 3: return 'Апр'; case 4: return 'Май'; case 5: return 'Июн'; case 6: return 'Июл'; case 7: return 'Авг'; case 8: return 'Сен'; case 9: return 'Окт'; case 10: return 'Ноя'; case 11: return 'Дек'; } } /** * @param dayNumber * @returns {string} */ static getDayShortName(dayNumber) { switch (dayNumber) { case 0: return 'Вс'; case 1: return 'Пн'; case 2: return 'Вт'; case 3: return 'Ср'; case 4: return 'Чт'; case 5: return 'Пт'; case 6: return 'Сб'; } } /** * @param dateObj * @returns {string} */ static toSQLDate(dateObj) { var yearStr = String(dateObj.getFullYear()); var month = dateObj.getMonth() + 1; var monthStr = month < 10 ? `0${month}` : String(month); var date = dateObj.getDate(); var dateStr = date < 10 ? `0${date}` : String(date); return [yearStr, monthStr, dateStr].join('-'); } /** * @param string * @returns {Date} */ static createDateFromSQLFormat(sqlDate) { let [dateStr, timeStr] = sqlDate.split(' '); let dateArr = dateStr.split('-'); let date = new Date(dateArr[0], dateArr[1] - 1, dateArr[2]); if (timeStr) { let timeArr = timeStr.split(':'); date.setHours(timeArr[0]); date.setMinutes(timeArr[1]); date.setSeconds(timeArr[2]); } return date; } }