Last active
September 17, 2024 09:47
-
-
Save artemsites/2dbe12ad474772bc9cf73b77cedf1548 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @version 17.09.2024 | |
* @author [email protected] | |
* import { DateFormatterClass } from "/src/utils/DateFormatterClass.js" | |
* | |
* const dateFormatter = new DateFormatterClass('2024-03-28') // создаст экземпляр на указанную дату | |
* const dateFormatter = new DateFormatterClass() // создаст экземпляр на текущую дату | |
*/ | |
export class DateFormatterClass { | |
constructor(dateString = new Date()) { | |
this.date = new Date(dateString) | |
} | |
// Ср | |
getNameDayWeekShort(date) { | |
if (date) this.date = new Date(date) | |
const daysOfWeek = ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'] | |
return daysOfWeek[this.date.getDay()] | |
} | |
// 28.03 | |
getDateDayAndMonth(date) { | |
if (date) this.date = new Date(date) | |
let day = this.date.getDate().toString() | |
if (day.length < 2) { | |
day = '0' + day | |
} | |
let month = (this.date.getMonth() + 1).toString() | |
if (month.length < 2) { | |
month = '0' + month | |
} | |
return `${day}.${month}` | |
} | |
// 2024-08-15 | |
getDateCurrent() { | |
return new Date().toISOString().slice(0, 10) | |
} | |
// 1723604400 | |
getTimestampInSeconds() { | |
return Math.floor(Date.now() / 1000) | |
} | |
// 1724428867026 | |
getTimestamp() { | |
return Date.now() | |
} | |
// гггг-мм-дд -> дд.мм.гггг | |
fromIsoToDmy(isoDateStr) { | |
// console.log('isoDateStr') | |
// console.log(isoDateStr) | |
const regex = /^\d{4}-\d{2}-\d{2}$/ | |
if (!regex.test(isoDateStr)) { | |
return isoDateStr | |
// throw new Error("Неверный формат даты. Ожидается гггг-мм-дд.") | |
} | |
// console.log("isoDateStr.split("-")") | |
// console.log(isoDateStr.split("-")) | |
const [yyyy, mm, dd] = isoDateStr.split("-") | |
return `${dd}.${mm}.${yyyy}` | |
} | |
// гггг-мм-дд -> дд.мм.гггг | |
fromDmyToIso(dmyDateStr) { | |
const regex = /^\d{2}\.\d{2}\.\d{4}$/ | |
if (!regex.test(dmyDateStr)) { | |
// throw new Error("Неверный формат даты. Ожидается дд.мм.гггг.") | |
return dmyDateStr | |
} | |
const [dd, mm, yyyy] = dmyDateStr.split(".") | |
return `${yyyy}-${mm}-${dd}` | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment