date-fns#differenceInMonths JavaScript Examples
The following examples show how to use
date-fns#differenceInMonths.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: relativeCommitTimeCalculator.js From gitconvex with Apache License 2.0 | 6 votes |
export function relativeCommitTimeCalculator(commitTime) {
let commitRelativeTime;
const days = differenceInCalendarDays(new Date(), new Date(commitTime));
const hours = differenceInHours(new Date(), new Date(commitTime));
const minutes = differenceInMinutes(new Date(), new Date(commitTime));
if (days > 0) {
if (days >= 30) {
const month = differenceInMonths(new Date(), new Date(commitTime));
commitRelativeTime =
month === 1 ? month + " Month Ago" : month + " Months Ago";
} else if (days >= 365) {
const year = differenceInYears(new Date(), new Date(commitTime));
commitRelativeTime =
year === 1 ? year + " Year Ago" : year + " Years Ago";
} else {
commitRelativeTime = days === 1 ? days + " Day Ago" : days + " Days Ago";
}
} else if (hours > 0) {
commitRelativeTime =
hours === 1 ? hours + " Hour Ago" : hours + " Hours Ago";
} else if (minutes > 0) {
commitRelativeTime =
minutes === 1 ? minutes + " Minute Ago" : minutes + " Minutes Ago";
} else {
commitRelativeTime = "recent commit";
}
return commitRelativeTime;
}
Example #2
Source File: MonthView.js From react-horizontal-datepicker with MIT License | 5 votes |
MonthView = ({startDate, lastDate, selectDate, getSelectedDay, primaryColor, labelFormat}) => {
const [selectedDate, setSelectedDate] = useState(null);
const rgb = primaryColor.replace(/[^\d,]/g, '').split(',');
const brightness = Math.round(((parseInt(rgb[0]) * 299) +
(parseInt(rgb[1]) * 587) +
(parseInt(rgb[2]) * 114)) / 1000);
const textColour = (brightness > 125) ? 'black' : 'white';
const selectedStyle = {borderRadius:"0.7rem",background:`${primaryColor}`, color: textColour};
const getStyles = (day) => {
return isSameDay(day, selectedDate)?selectedStyle:null;
};
const getId = (day) => {
return isSameDay(day, selectedDate)?'selected':"";
};
const renderDays = () => {
const months = [];
for (let i = 0; i <= differenceInMonths(lastDate, startDate); i++) {
const month = startOfMonth(addMonths(startDate, i));
months.push(
<div id={`${getId(month)}`}
className={styles.monthContainer}
key={month}
style={getStyles(month)}
onClick={() => onDateClick(month)}
>
<span className={styles.monthYearLabel}>
{format(month, labelFormat || "MMMM yyyy")}
</span>
</div>
);
}
return <div id={"container"} className={styles.dateListScrollable}>{months}</div>;
}
const onDateClick = day => {
setSelectedDate(day);
if (getSelectedDay) {
getSelectedDay(day);
}
};
useEffect(() => {
if (getSelectedDay) {
if (selectDate) {
getSelectedDay(selectDate);
} else {
getSelectedDay(startDate);
}
}
}, []);
useEffect(() => {
if (selectDate) {
if (!isSameDay(selectedDate, selectDate)) {
setSelectedDate(selectDate);
setTimeout(() => {
let view = document.getElementById('selected');
if (view) {
view.scrollIntoView({behavior: "smooth", inline: "center", block: "nearest"});
}
}, 20);
}
}
}, [selectDate]);
return <React.Fragment>{renderDays()}</React.Fragment>
}
Example #3
Source File: DateView.js From react-horizontal-datepicker with MIT License | 4 votes |
DateView = ({
startDate,
lastDate,
selectDate,
getSelectedDay,
primaryColor,
labelFormat
}) => {
const [selectedDate, setSelectedDate] = useState(null);
const firstSection = {
marginLeft: '40px'
};
const selectedStyle = {
fontWeight: "bold",
width: "45px",
height: "45px",
borderRadius: "50%",
border: `2px solid ${primaryColor}`,
color: primaryColor
};
const labelColor = {
color: primaryColor
};
const getStyles = day => {
return isSameDay(day, selectedDate) ? selectedStyle : null;
};
const getId = day => {
return isSameDay(day, selectedDate) ? 'selected' : "";
};
const renderDays = () => {
const dayFormat = "E";
const dateFormat = "d";
const months = [];
let days = [];
for (let i = 0; i <= differenceInMonths(lastDate, startDate); i++) {
let start, end;
const month = startOfMonth(addMonths(startDate, i));
start = i === 0 ? Number(format(startDate, dateFormat)) - 1 : 0;
end = i === differenceInMonths(lastDate, startDate) ? Number(format(lastDate, "d")) : Number(format(lastDayOfMonth(month), "d"));
for (let j = start; j < end; j++) {
let currentDay = addDays(month, j);
days.push( /*#__PURE__*/React.createElement("div", {
id: `${getId(currentDay)}`,
className: styles.dateDayItem,
style: getStyles(currentDay),
key: currentDay,
onClick: () => onDateClick(currentDay)
}, /*#__PURE__*/React.createElement("div", {
className: styles.dayLabel
}, format(currentDay, dayFormat)), /*#__PURE__*/React.createElement("div", {
className: styles.dateLabel
}, format(currentDay, dateFormat))));
}
months.push( /*#__PURE__*/React.createElement("div", {
className: styles.monthContainer,
key: month
}, /*#__PURE__*/React.createElement("span", {
className: styles.monthYearLabel,
style: labelColor
}, format(month, labelFormat || "MMMM yyyy")), /*#__PURE__*/React.createElement("div", {
className: styles.daysContainer,
style: i === 0 ? firstSection : null
}, days)));
days = [];
}
return /*#__PURE__*/React.createElement("div", {
id: "container",
className: styles.dateListScrollable
}, months);
};
const onDateClick = day => {
setSelectedDate(day);
if (getSelectedDay) {
getSelectedDay(day);
}
};
useEffect(() => {
if (getSelectedDay) {
if (selectDate) {
getSelectedDay(selectDate);
} else {
getSelectedDay(startDate);
}
}
}, []);
useEffect(() => {
if (selectDate) {
if (!isSameDay(selectedDate, selectDate)) {
setSelectedDate(selectDate);
setTimeout(() => {
let view = document.getElementById('selected');
if (view) {
view.scrollIntoView({
behavior: "smooth",
inline: "center",
block: "nearest"
});
}
}, 20);
}
}
}, [selectDate]);
return /*#__PURE__*/React.createElement(React.Fragment, null, renderDays());
}
Example #4
Source File: MonthView.js From react-horizontal-datepicker with MIT License | 4 votes |
MonthView = ({
startDate,
lastDate,
selectDate,
getSelectedDay,
primaryColor,
labelFormat
}) => {
const [selectedDate, setSelectedDate] = useState(null);
const rgb = primaryColor.replace(/[^\d,]/g, '').split(',');
const brightness = Math.round((parseInt(rgb[0]) * 299 + parseInt(rgb[1]) * 587 + parseInt(rgb[2]) * 114) / 1000);
const textColour = brightness > 125 ? 'black' : 'white';
const selectedStyle = {
borderRadius: "0.7rem",
background: `${primaryColor}`,
color: textColour
};
const getStyles = day => {
return isSameDay(day, selectedDate) ? selectedStyle : null;
};
const getId = day => {
return isSameDay(day, selectedDate) ? 'selected' : "";
};
const renderDays = () => {
const months = [];
for (let i = 0; i <= differenceInMonths(lastDate, startDate); i++) {
const month = startOfMonth(addMonths(startDate, i));
months.push( /*#__PURE__*/React.createElement("div", {
id: `${getId(month)}`,
className: styles.monthContainer,
key: month,
style: getStyles(month),
onClick: () => onDateClick(month)
}, /*#__PURE__*/React.createElement("span", {
className: styles.monthYearLabel
}, format(month, labelFormat || "MMMM yyyy"))));
}
return /*#__PURE__*/React.createElement("div", {
id: "container",
className: styles.dateListScrollable
}, months);
};
const onDateClick = day => {
setSelectedDate(day);
if (getSelectedDay) {
getSelectedDay(day);
}
};
useEffect(() => {
if (getSelectedDay) {
if (selectDate) {
getSelectedDay(selectDate);
} else {
getSelectedDay(startDate);
}
}
}, []);
useEffect(() => {
if (selectDate) {
if (!isSameDay(selectedDate, selectDate)) {
setSelectedDate(selectDate);
setTimeout(() => {
let view = document.getElementById('selected');
if (view) {
view.scrollIntoView({
behavior: "smooth",
inline: "center",
block: "nearest"
});
}
}, 20);
}
}
}, [selectDate]);
return /*#__PURE__*/React.createElement(React.Fragment, null, renderDays());
}
Example #5
Source File: DateView.js From react-horizontal-datepicker with MIT License | 4 votes |
DateView = ({startDate, lastDate, selectDate, getSelectedDay, primaryColor, labelFormat, marked}) => {
const [selectedDate, setSelectedDate] = useState(null);
const firstSection = {marginLeft: '40px'};
const selectedStyle = {fontWeight:"bold",width:"45px",height:"45px",borderRadius:"50%",border:`2px solid ${primaryColor}`,color:primaryColor};
const labelColor = {color: primaryColor};
const markedStyle = {color: "#8c3737", padding: "2px", fontSize: 12};
const getStyles = (day) => {
return isSameDay(day, selectedDate)?selectedStyle:null;
};
const getId = (day) => {
return isSameDay(day, selectedDate)?'selected':"";
};
const getMarked = (day) => {
let markedRes = marked.find(i => isSameDay(i.date, day));
if (markedRes) {
if (!markedRes?.marked) {
return;
}
return <div style={{ ...markedRes?.style ?? markedStyle }} className={styles.markedLabel}>
{markedRes.text}
</div>;
}
return "";
};
const renderDays = () => {
const dayFormat = "E";
const dateFormat = "d";
const months = [];
let days = [];
// const styleItemMarked = marked ? styles.dateDayItemMarked : styles.dateDayItem;
for (let i = 0; i <= differenceInMonths(lastDate, startDate); i++) {
let start, end;
const month = startOfMonth(addMonths(startDate, i));
start = i === 0 ? Number(format(startDate, dateFormat)) - 1 : 0;
end = i === differenceInMonths(lastDate, startDate) ? Number(format(lastDate, "d")) : Number(format(lastDayOfMonth(month), "d"));
for (let j = start; j < end; j++) {
let currentDay = addDays(month, j);
days.push(
<div id={`${getId(currentDay)}`}
className={marked ? styles.dateDayItemMarked : styles.dateDayItem}
style={getStyles(currentDay)}
key={currentDay}
onClick={() => onDateClick(currentDay)}
>
<div className={styles.dayLabel}>{format(currentDay, dayFormat)}</div>
<div className={styles.dateLabel}>{format(currentDay, dateFormat)}</div>
{getMarked(currentDay)}
</div>
);
}
months.push(
<div className={styles.monthContainer}
key={month}
>
<span className={styles.monthYearLabel} style={labelColor}>
{format(month, labelFormat || "MMMM yyyy")}
</span>
<div className={styles.daysContainer} style={i===0?firstSection:null}>
{days}
</div>
</div>
);
days = [];
}
return <div id={"container"} className={styles.dateListScrollable}>{months}</div>;
}
const onDateClick = day => {
setSelectedDate(day);
if (getSelectedDay) {
getSelectedDay(day);
}
};
useEffect(() => {
if (getSelectedDay) {
if (selectDate) {
getSelectedDay(selectDate);
} else {
getSelectedDay(startDate);
}
}
}, []);
useEffect(() => {
if (selectDate) {
if (!isSameDay(selectedDate, selectDate)) {
setSelectedDate(selectDate);
setTimeout(() => {
let view = document.getElementById('selected');
if (view) {
view.scrollIntoView({behavior: "smooth", inline: "center", block: "nearest"});
}
}, 20);
}
}
}, [selectDate]);
return <React.Fragment>{renderDays()}</React.Fragment>
}