@chayns-components/date
Version:
A set of beautiful React components for developing your own applications with chayns.
114 lines • 3.99 kB
JavaScript
export const isToday = date => {
const today = new Date();
return today.toDateString() === date.toDateString();
};
export const isTomorrow = date => {
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
return tomorrow.toDateString() === date.toDateString();
};
export const isYesterday = date => {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
return yesterday.toDateString() === date.toDateString();
};
export const isCurrentYear = date => {
const currentYear = new Date().getFullYear();
const yearOfGivenDate = date.getFullYear();
return currentYear === yearOfGivenDate;
};
export const getIsDateNearToday = date => {
const today = new Date();
today.setHours(0, 0, 0, 0);
const targetDate = new Date(date);
targetDate.setHours(0, 0, 0, 0);
const diffInDays = (targetDate.getTime() - today.getTime()) / (1000 * 60 * 60 * 24);
return diffInDays === 0 || diffInDays === -1 || diffInDays === 1;
};
export const isMorning = date => {
const hours = date.getHours();
return hours >= 0 && hours < 12;
};
export const isAfter = (firstDate, secondDate) => {
return firstDate.getTime() > secondDate.getTime();
};
export const isBefore = (firstDate, secondDate) => {
return firstDate.getTime() < secondDate.getTime();
};
export const startOfMonth = date => {
return new Date(date.getFullYear(), date.getMonth(), 1);
};
export const addYears = (date, years) => {
return new Date(date.getFullYear() + years, date.getMonth(), date.getDate());
};
export const differenceInCalendarMonths = (firstDate, secondDate) => {
return (firstDate.getFullYear() - secondDate.getFullYear()) * 12 + (firstDate.getMonth() - secondDate.getMonth());
};
export const isSameDay = (firstDate, secondDate) => {
return firstDate.getFullYear() === secondDate.getFullYear() && firstDate.getMonth() === secondDate.getMonth() && firstDate.getDate() === secondDate.getDate();
};
export const isSameMonth = (firstDate, secondDate) => {
return firstDate.getFullYear() === secondDate.getFullYear() && firstDate.getMonth() === secondDate.getMonth();
};
export const isWithinInterval = (date, interval) => {
return date.getTime() >= interval.start.getTime() && date.getTime() <= interval.end.getTime();
};
export const subYears = (date, years) => {
return new Date(date.getFullYear() - years, date.getMonth(), date.getDate());
};
export const startOfWeek = date => {
const day = date.getDay();
const diff = day === 0 ? -6 : 1 - day;
const start = new Date(date);
start.setDate(date.getDate() + diff);
start.setHours(0, 0, 0, 0);
return start;
};
export const endOfWeek = date => {
const day = date.getDay();
const diff = day === 0 ? 0 : 7 - day;
const end = new Date(date);
end.setDate(date.getDate() + diff);
end.setHours(23, 59, 59, 999);
return end;
};
export const eachDayOfInterval = interval => {
const days = [];
const currentDate = new Date(interval.start);
while (currentDate <= interval.end) {
days.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
return days;
};
export const addDays = (date, days) => {
const result = new Date(date);
result.setDate(date.getDate() + days);
return result;
};
export const subDays = (date, days) => {
const result = new Date(date);
result.setDate(date.getDate() - days);
return result;
};
export const addHours = (date, hours) => {
const result = new Date(date);
result.setHours(date.getHours() + hours);
return result;
};
export const addMinutes = (date, minutes) => {
const result = new Date(date);
result.setMinutes(date.getMinutes() + minutes);
return result;
};
export const subHours = (date, hours) => {
const result = new Date(date);
result.setHours(date.getHours() - hours);
return result;
};
export const subMinutes = (date, minutes) => {
const result = new Date(date);
result.setMinutes(date.getMinutes() - minutes);
return result;
};
//# sourceMappingURL=date.js.map