2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
2023-01-16 13:44:52 +05:00
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
2022-08-31 06:33:37 +05:00
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2022-08-30 16:13:11 +05:00
|
|
|
|
2023-06-01 20:56:02 +05:00
|
|
|
import dayjs from "dayjs";
|
|
|
|
|
|
2019-12-12 11:49:06 +05:00
|
|
|
export function getWeekGroupFromTimestamp(timestamp) {
|
2022-06-16 16:01:08 +05:00
|
|
|
const date = new Date(timestamp);
|
|
|
|
|
const { start, end } = getWeek(date);
|
|
|
|
|
|
|
|
|
|
const startMonth =
|
|
|
|
|
start.month !== end.month ? " " + MONTHS_SHORT[start.month] : "";
|
|
|
|
|
const startYear = start.year !== end.year ? ", " + start.year : "";
|
|
|
|
|
|
|
|
|
|
const startDate = `${start.day}${startMonth}${startYear}`;
|
|
|
|
|
const endDate = `${end.day} ${MONTHS_SHORT[end.month]}, ${end.year}`;
|
|
|
|
|
|
|
|
|
|
return `${startDate} - ${endDate}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MS_IN_HOUR = 3600000;
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {Date} date
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
function getWeek(date) {
|
|
|
|
|
var day = date.getDay() || 7;
|
|
|
|
|
if (day !== 1) {
|
|
|
|
|
const hours = 24 * (day - 1);
|
|
|
|
|
date.setTime(date.getTime() - MS_IN_HOUR * hours);
|
|
|
|
|
}
|
|
|
|
|
const start = {
|
|
|
|
|
month: date.getMonth(),
|
|
|
|
|
year: date.getFullYear(),
|
2022-08-31 06:33:37 +05:00
|
|
|
day: date.getDate()
|
2022-06-16 16:01:08 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hours = 24 * 6;
|
|
|
|
|
date.setTime(date.getTime() + MS_IN_HOUR * hours);
|
|
|
|
|
|
|
|
|
|
const end = {
|
|
|
|
|
month: date.getMonth(),
|
|
|
|
|
year: date.getFullYear(),
|
2022-08-31 06:33:37 +05:00
|
|
|
day: date.getDate()
|
2022-06-16 16:01:08 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return { start, end };
|
2019-12-12 11:49:06 +05:00
|
|
|
}
|
2020-08-31 13:12:09 +05:00
|
|
|
|
2021-12-22 10:07:23 +05:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {number} date
|
2023-06-01 20:56:02 +05:00
|
|
|
* @param {{dateFormat: string, timeFormat: string, type: "date-time" | "time" | "date"}} options
|
2021-12-22 10:07:23 +05:00
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export function formatDate(
|
|
|
|
|
date,
|
|
|
|
|
options = {
|
2023-06-01 20:56:02 +05:00
|
|
|
dateFormat: "DD-MM-YYYY",
|
|
|
|
|
timeFormat: "12-hour",
|
|
|
|
|
type: "date-time"
|
2021-12-22 10:07:23 +05:00
|
|
|
}
|
|
|
|
|
) {
|
2023-06-01 21:27:15 +05:00
|
|
|
const timeFormat = options.timeFormat === "12-hour" ? "h:mm A" : "HH:mm";
|
2023-06-01 20:56:02 +05:00
|
|
|
switch (options.type) {
|
|
|
|
|
case "date-time":
|
2023-06-01 21:27:15 +05:00
|
|
|
return dayjs(date).format(`${options.dateFormat} ${timeFormat}`);
|
2023-06-01 20:56:02 +05:00
|
|
|
case "time":
|
2023-06-01 21:27:15 +05:00
|
|
|
return dayjs(date).format(timeFormat);
|
2023-06-01 20:56:02 +05:00
|
|
|
case "date":
|
|
|
|
|
return dayjs(date).format(`${options.dateFormat}`);
|
|
|
|
|
}
|
2020-08-31 13:12:09 +05:00
|
|
|
}
|
2022-06-16 16:01:08 +05:00
|
|
|
|
|
|
|
|
export const MONTHS_FULL = [
|
|
|
|
|
"January",
|
|
|
|
|
"February",
|
|
|
|
|
"March",
|
|
|
|
|
"April",
|
|
|
|
|
"May",
|
|
|
|
|
"June",
|
|
|
|
|
"July",
|
|
|
|
|
"August",
|
|
|
|
|
"September",
|
|
|
|
|
"October",
|
|
|
|
|
"November",
|
2022-08-31 06:33:37 +05:00
|
|
|
"December"
|
2022-06-16 16:01:08 +05:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const MONTHS_SHORT = [
|
|
|
|
|
"Jan",
|
|
|
|
|
"Feb",
|
|
|
|
|
"Mar",
|
|
|
|
|
"Apr",
|
|
|
|
|
"May",
|
|
|
|
|
"Jun",
|
|
|
|
|
"Jul",
|
|
|
|
|
"Aug",
|
|
|
|
|
"Sep",
|
|
|
|
|
"Oct",
|
|
|
|
|
"Nov",
|
2022-08-31 06:33:37 +05:00
|
|
|
"Dec"
|
2022-06-16 16:01:08 +05:00
|
|
|
];
|