date-fns#differenceInWeeks TypeScript Examples
The following examples show how to use
date-fns#differenceInWeeks.
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: dategrid.ts From calendar-hack with MIT License | 6 votes |
setEvent(date: Date, event: T | undefined) {
const k = key(date);
if (event) {
this._events.set(k, event);
// min/max/first/last/weekCount maintenance
if (!this._max || isAfter(date, this._max))
this._max = date;
this._last = startOfDay(endOfWeek(this._max, { weekStartsOn: 1 }))
if (!this._min || isBefore(date, this._min))
this._min = date;
this._first = startOfWeek(this._min, { weekStartsOn: 1 })
this._weekCount = differenceInWeeks(startOfDay(addDays(this._last, 1)), this._first);
} else {
this._events.delete(k);
}
}
Example #2
Source File: WeekTimeline.tsx From life-calendar with MIT License | 4 votes |
transformData = (data: any = { events: [] }) => {
const _data = JSON.parse(JSON.stringify(data));
let firstEvent: any = {
...DefaultData.events[0],
_date: parseDate(DefaultData.events[0].date)
}; // default 1st event (as the "base" event)
_data.events = _data.events || [];
// for each item, calculate and append more properties:
_data.events = _data.events.map((item: any, idx: number) => {
item._date = parseDate(item.date);
if (idx === 0) {
firstEvent = { ...item };
item._weekNum = 0;
} else {
item._weekNum = differenceInWeeks(item._date, firstEvent._date);
}
return item;
});
// append "Today" to the _data.events (as the last event)
const now = new Date();
_data.events.push({
title: `Today`,
type: 0,
date: format(now, 'yyyy-MM-dd'),
_date: now,
_weekNum: differenceInWeeks(now, firstEvent._date)
});
const firstYear = getYear(firstEvent._date);
let markedWeeks = []; // highlighted boxes - [0, 52, 104, ...] for 90 years
let weeks = 0;
for (let i = 0; i < 90; i += 1) {
const year = firstYear + i;
weeks = weeks + (isLeapYear(year) ? 52.28571 : 52.14286);
let markedWeek = Math.round(weeks);
// adjust for better alignment (because of Math.round and leap years)
markedWeek++;
if (
[
4,
5,
6,
11,
12,
13,
18,
19,
20,
25,
26,
27,
32,
33,
34,
39,
40,
41,
46,
47,
48,
53,
54,
55,
60,
61,
62,
67,
68,
69,
74,
75,
76,
81,
82,
83,
88,
89,
90
].indexOf(i + 1) >= 0
) {
markedWeek = markedWeek - 1;
}
markedWeeks.push(markedWeek); // TODO: rounding leads to inaccurate box position
}
// console.log('markedWeeks', markedWeeks);
return [_data, markedWeeks];
}