mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-12 13:49:22 -05:00
* Issue 858: On the new calendar page adding a new Date entry with the "Add" button will now insert a new date with an offset of +24 hours of the previous row's date value. * Added comment to ignore the TSLint "object can be null" error * Changed `var` to `const` * Fixed Prettier checks in CI pipeline * Fixed Typecheck CI pipeline error * Moved getDateWithHoursOffset() function to app/utils/dates.ts * Added new line at end of file * Added getValidNewDateIfInvalid() function. This retrieves a valid date. If invalid, get a new Date object. - So now, if we intentionally/accidentally delete the data in the Calendar's DateInput element, it will be reset to the current Date/Time * Refactored DateInput component's update state mechanism to be handled by an onChange() function defined in the parent component that is passed to the child * Prettier formatting so that the new CI pipeline won't output errors at me * Removed unused imported types * Removed the datesCount React Hook & refactored accordingly * Removed unused loader-related variables * DateInput onChange prop is now optional * Instead of generating a new Array, iterate over DateInput's inputState's array instead * Fix potential undefined error * DatesInputState: refactored to remove index & access the index during iteration with map() 2nd arg * Properly initialized state for pre-existing events =) - Also added TODO comments for improving date input handling (1082) * Prettier formatting * Uncommented console.warn() * Touched up comment
84 lines
2.2 KiB
TypeScript
84 lines
2.2 KiB
TypeScript
import { getWeek } from "date-fns";
|
|
|
|
export function databaseTimestampToDate(timestamp: number) {
|
|
return new Date(timestamp * 1000);
|
|
}
|
|
|
|
export function dateToDatabaseTimestamp(date: Date) {
|
|
return Math.floor(date.getTime() / 1000);
|
|
}
|
|
|
|
export function dateToWeekNumber(date: Date) {
|
|
return getWeek(date, { weekStartsOn: 1, firstWeekContainsDate: 4 });
|
|
}
|
|
|
|
// https://stackoverflow.com/a/71336659
|
|
export function weekNumberToDate({
|
|
week,
|
|
year,
|
|
position = "start",
|
|
}: {
|
|
week: number;
|
|
year: number;
|
|
/** start = Date of Monday, end = Date of Sunday */
|
|
position?: "start" | "end";
|
|
}) {
|
|
const result = new Date(Date.UTC(year, 0, 4));
|
|
|
|
result.setDate(
|
|
result.getDate() - (result.getDay() || 7) + 1 + 7 * (week - 1)
|
|
);
|
|
if (position === "end") {
|
|
result.setDate(result.getDate() + 6);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Checks if a date is valid or not.
|
|
*
|
|
* Returns:
|
|
* - True if date is valid
|
|
* - False otherwise
|
|
*/
|
|
export function isValidDate(date: Date) {
|
|
return !isNaN(date.getTime());
|
|
}
|
|
|
|
/** Returns date as a string with the format YYYY-MM-DDThh:mm in user's time zone */
|
|
export function dateToYearMonthDayHourMinuteString(date: Date) {
|
|
const copiedDate = new Date(date.getTime());
|
|
|
|
//TODO: fix invalid Date Input handling: https://github.com/Sendouc/sendou.ink/issues/1082
|
|
if (!isValidDate(copiedDate)) {
|
|
console.warn("Invalid date");
|
|
// throw new RangeError("Invalid Date");
|
|
}
|
|
|
|
const year = copiedDate.getFullYear();
|
|
const month = copiedDate.getMonth() + 1;
|
|
const day = copiedDate.getDate();
|
|
const hour = copiedDate.getHours();
|
|
const minute = copiedDate.getMinutes();
|
|
|
|
return `${year}-${prefixZero(month)}-${prefixZero(day)}T${prefixZero(
|
|
hour
|
|
)}:${prefixZero(minute)}`;
|
|
}
|
|
|
|
function prefixZero(number: number) {
|
|
return number < 10 ? `0${number}` : number;
|
|
}
|
|
|
|
/**
|
|
* Retrieves a new Date object that is offset by several hours.
|
|
*
|
|
* NOTE: it is important that we work with & return a copy of the date here,
|
|
* otherwise we will just be mutating the original date passed into this function.
|
|
*/
|
|
export function getDateWithHoursOffset(date: Date, hoursOffset: number) {
|
|
const copiedDate = new Date(date.getTime());
|
|
copiedDate.setHours(date.getHours() + hoursOffset);
|
|
return copiedDate;
|
|
}
|