guild_front/src/components/Calendar/calendarHelper.js

109 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-05-18 17:42:29 +03:00
import moment from "moment";
import "moment/locale/ru";
2021-06-22 18:00:52 +03:00
export function calendarHelper(value) {
2023-05-18 17:42:29 +03:00
const startDay = value
.clone()
.startOf("month")
.startOf("week")
.startOf("day");
const endDay = value.clone().endOf("month").endOf("week");
2021-06-17 18:21:48 +03:00
2023-05-18 17:42:29 +03:00
const day = startDay.clone().subtract(1, "day");
2021-06-17 18:21:48 +03:00
const calendar = [];
2023-05-18 17:42:29 +03:00
while (day.isBefore(endDay, "day")) {
2021-06-17 18:21:48 +03:00
calendar.push(
2021-06-18 17:16:08 +03:00
Array(1)
2021-06-17 18:21:48 +03:00
.fill(0)
2023-12-05 14:15:04 +03:00
.map(() => day.add(1, "day").clone())
2021-06-17 18:21:48 +03:00
);
}
return calendar;
}
2021-06-22 18:00:52 +03:00
2022-12-26 15:12:01 +03:00
export function getReports(value) {
2023-05-18 17:42:29 +03:00
const startDay = value
.clone()
.startOf("month")
.startOf("week")
.startOf("day");
const reportsStart = `${new Date(startDay).getFullYear()}-${
new Date(startDay).getMonth() + 1
}-${new Date(startDay).getDate()}`;
const endDay = value.clone().endOf("month").endOf("week");
const reportsEnd = `${new Date(endDay).getFullYear()}-${
new Date(endDay).getMonth() + 1
}-${new Date(endDay).getDate()}`;
const getReports = `fromDate=${reportsStart}&toDate=${reportsEnd}`;
2022-12-26 15:12:01 +03:00
return getReports;
}
2023-02-17 15:19:49 +03:00
export function getCreatedDate(day) {
if (day) {
2023-05-18 17:42:29 +03:00
return `${new Date(day).getFullYear()}-${correctDay(
2023-12-05 14:15:04 +03:00
new Date(day).getMonth() + 1
2023-05-18 17:42:29 +03:00
)}-${correctDay(new Date(day).getDate())}`;
2023-02-17 15:19:49 +03:00
} else {
const date = new Date();
2023-05-18 17:42:29 +03:00
const dd = String(date.getDate()).padStart(2, "0");
const mm = String(date.getMonth() + 1).padStart(2, "0");
2023-02-17 15:19:49 +03:00
const yyyy = date.getFullYear();
2023-05-18 17:42:29 +03:00
return `${yyyy}-${mm}-${dd}`;
2023-02-17 15:19:49 +03:00
}
}
2023-05-18 17:42:29 +03:00
export function correctDay(day) {
if (day < 10) {
return `0${day}`;
}
return day;
}
2021-06-22 18:00:52 +03:00
export function currentMonth() {
2023-05-18 17:42:29 +03:00
const currentMonth = moment().format("MMMM");
2021-06-22 18:00:52 +03:00
return currentMonth.charAt(0).toUpperCase() + currentMonth.slice(1);
}
2021-06-23 12:48:02 +03:00
export function currentMonthAndDay(day) {
2023-05-18 17:42:29 +03:00
return day.format("D MMMM");
2021-06-23 12:48:02 +03:00
}
2021-06-22 18:00:52 +03:00
2023-02-17 15:19:49 +03:00
export function getCorrectDate(day) {
2023-05-18 17:42:29 +03:00
const months = [
"января",
"февраля",
"марта",
"апреля",
"мая",
"июня",
"июля",
"августа",
"сентября",
"октября",
"ноября",
2023-12-19 17:36:30 +03:00
"декабря"
2023-05-18 17:42:29 +03:00
];
return `${new Date(day).getDate()} ${
months[new Date(day).getMonth()]
} ${new Date(day).getFullYear()} года`;
}
2023-02-17 15:19:49 +03:00
2021-06-23 12:48:02 +03:00
export function currentMonthAndDayReportPage() {
2023-05-18 17:42:29 +03:00
return moment().format("D MMMM");
2022-12-26 15:12:01 +03:00
}
2023-03-01 00:28:58 +03:00
export function hourOfNum(number) {
2023-05-18 17:42:29 +03:00
const hours = [" час", " часа", " часов"];
2023-03-01 00:28:58 +03:00
const cases = [2, 0, 1, 1, 1, 2];
2023-05-18 17:42:29 +03:00
return hours[
number % 100 > 4 && number % 100 < 20
? 2
: cases[number % 10 < 5 ? number % 10 : 5]
];
2023-03-01 00:28:58 +03:00
}