/
laborgit
/
laborgit-frontend
Обзор
Документация
Войти
/
laborgit
/
laborgit-frontend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
main
src/utils/datetime.ts
106 строк
3 KB
gdev2018
TSKLBRGTFRNT-26 upd sort by last_step_updated
23 ноя 2025, 16:13
23 ноя 2025, 16:13
b93d0e7
Код
Авторство
О чём код?
import humanizeDuration from "humanize-duration"; import moment, { Moment } from "moment"; export function formatDuration(durationInSeconds?: number): string { if (typeof durationInSeconds === "undefined") { return "0"; } return humanizeDuration(durationInSeconds * 1000, { language: "en", units: ["h", "m"], round: true }); } export const nowUnixTimeUtc = Math.floor(Date.now() / 1000); export function formatDateToYYYYMMDD(date: Date): string { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, "0"); const day = String(date.getDate()).padStart(2, "0"); return `${year}-${month}-${day}`; } export const formatTimestampToYYYYMMDDHHmm = ( timestamp: string | number | undefined ): string | null => { if (!timestamp) return null; const numericTimestamp = typeof timestamp === "number" ? timestamp : parseInt(timestamp, 10); const date = new Date(numericTimestamp * 1000); if (isNaN(date.getTime())) return "Invalid date"; const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, "0"); const day = String(date.getDate()).padStart(2, "0"); const hours = String(date.getHours()).padStart(2, "0"); const minutes = String(date.getMinutes()).padStart(2, "0"); return `${year}-${month}-${day} ${hours}:${minutes}`; }; /** * Returns the start and end dates of the week for the given date. * @param inputDate - Date (string, Date, or Moment object) * @param weekStartsOn - Day the week starts on (0 - Sunday, 1 - Monday, default is 1) * @returns { start: string; end: string } - Object containing start and end dates of the week */ export function getWeekRange( inputDate: string | Date | Moment, weekStartsOn: 0 | 1 = 1 ): { start: string; end: string } { const date = moment(inputDate); if (!date.isValid()) { throw new Error("Invalid date"); } // Determine week start (Monday by default) const startOfWeek = date.clone().startOf("isoWeek"); if (weekStartsOn === 0) { startOfWeek.startOf("week"); // Use Sunday } const endOfWeek = startOfWeek.clone().add(6, "days"); return { start: startOfWeek.format("YYYY-MM-DD"), end: endOfWeek.format("YYYY-MM-DD") }; } export const getStartEndDates = (dateString: string, type: "day" | "week" | "month" | "year") => { const inputDate = new Date(dateString); let startDate: Date; let endDate: Date; switch (type) { case "year": startDate = new Date(inputDate.getFullYear(), 0, 1); endDate = new Date(inputDate.getFullYear() + 1, 0, 1); break; case "month": startDate = new Date(inputDate.getFullYear(), inputDate.getMonth(), 1); endDate = new Date(inputDate.getFullYear(), inputDate.getMonth() + 1, 1); break; case "week": { const weekRange = getWeekRange(dateString); startDate = new Date(weekRange.start); endDate = new Date(weekRange.end); endDate.setDate(endDate.getDate() + 1); break; } default: // day startDate = new Date(inputDate); endDate = new Date(inputDate); endDate.setDate(endDate.getDate() + 1); break; } return { startDate, endDate }; };