Files
notesnook/packages/core/utils/date.js

114 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-12-31 17:02:29 +05:00
function getWeeksInMonth(month, year, _start) {
let weeks = [],
2019-12-12 11:49:06 +05:00
firstDate = new Date(year, month, 1),
lastDate = new Date(year, month + 1, 0),
numDays = lastDate.getDate();
2019-12-31 17:02:29 +05:00
let start = 1;
let end = 7 - firstDate.getDay();
if (firstDate.getDay() === _start) {
end = 7;
} else {
let preMonthEndDay = new Date(year, month, 0);
start = preMonthEndDay.getDate() + 1 - firstDate.getDay() + _start;
end = 7 - firstDate.getDay() + _start;
weeks.push({
start: new Date(
preMonthEndDay.getFullYear(),
preMonthEndDay.getMonth(),
start
),
end: new Date(year, month, end),
2019-12-31 17:02:29 +05:00
});
start = end + 1;
end = end + 7;
}
2019-12-12 11:49:06 +05:00
while (start <= numDays) {
weeks.push({
start: new Date(year, month, start),
end: new Date(year, month, end),
2019-12-12 11:49:06 +05:00
});
start = end + 1;
end = end + 7;
2019-12-31 17:02:29 +05:00
if (end > numDays) {
end = end - numDays + _start;
weeks.push({
start: new Date(year, month, start),
end: new Date(year, month + 1, end),
2019-12-31 17:02:29 +05:00
});
break;
}
2019-12-12 11:49:06 +05:00
}
return weeks;
}
function getWeeksInYear(year) {
let weeks = [];
for (let i = 0; i <= 11; i++) {
2019-12-31 17:02:29 +05:00
weeks = weeks.concat(...getWeeksInMonth(i, year, 0));
2019-12-12 11:49:06 +05:00
}
return weeks;
}
export function get7DayTimestamp() {
return 604800000;
}
2019-12-12 11:49:06 +05:00
2019-12-14 16:15:35 +05:00
export function getLastWeekTimestamp() {
2019-12-12 11:49:06 +05:00
const t = new Date().getDate() + (6 - new Date().getDay() - 1) - 6;
const lastSaturday = new Date();
return lastSaturday.setDate(t);
}
export const months = [
2019-12-31 17:02:29 +05:00
"Jan",
"Feb",
"Mar",
"Apr",
2019-12-12 11:49:06 +05:00
"May",
2019-12-31 17:02:29 +05:00
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
2019-12-12 11:49:06 +05:00
];
let years = {};
export function getWeekGroupFromTimestamp(timestamp) {
let date = new Date(timestamp);
2019-12-31 17:02:29 +05:00
date.setHours(0, 0, 0, 0);
2019-12-12 11:49:06 +05:00
if (!years.hasOwnProperty(date.getFullYear())) {
2019-12-14 16:15:35 +05:00
years[date.getFullYear()] = getWeeksInYear(date.getFullYear());
2019-12-12 11:49:06 +05:00
}
let weeks = years[date.getFullYear()];
let week = weeks.find((v) => date >= v.start && date <= v.end);
2019-12-12 11:49:06 +05:00
//Format: {month} {start} - {end}, {year}
const startMonth =
week.start.getMonth() === week.end.getMonth()
? ""
: months[week.start.getMonth()];
const startYear =
week.start.getFullYear() === week.end.getFullYear()
? ""
: `, ${week.start.getFullYear()}`;
return `${startMonth} ${week.start.getDate()}${startYear} - ${
2019-12-31 17:02:29 +05:00
months[week.end.getMonth()]
} ${week.end.getDate()}, ${week.end.getFullYear()}`;
2019-12-12 11:49:06 +05:00
}
export function formatDate(date) {
return new Date(date).toLocaleDateString("en", {
day: "numeric",
month: "long",
year: "numeric",
hour: "2-digit",
hour12: true,
minute: "2-digit",
second: "2-digit",
});
}