fix date calculation bugs

This commit is contained in:
safwansamsudeen 2024-12-02 11:28:48 +05:30
parent 6e0e0a9704
commit 2eaa127c5b
4 changed files with 32 additions and 23 deletions

View File

@ -68,8 +68,8 @@
dependencies: 'Task 2', dependencies: 'Task 2',
}, },
{ {
start: '2024-04-08', start: '2024-03-08',
end: '2024-04-10', end: '2024-05-10',
name: 'Deploy', name: 'Deploy',
id: 'Task 4', id: 'Task 4',
progress: 0, progress: 0,
@ -108,7 +108,7 @@
// on_hover (task, x, y) { // on_hover (task, x, y) {
// console.log("Hover", x, y); // console.log("Hover", x, y);
// } // }
view_mode: 'Day', view_mode: 'Month',
// view_modes: [ // view_modes: [
// { // {
// name: 'Custom Day', // name: 'Custom Day',

View File

@ -505,11 +505,9 @@ export default class Bar {
const task_start = this.task._start; const task_start = this.task._start;
const gantt_start = this.gantt.gantt_start; const gantt_start = this.gantt.gantt_start;
const diff = date_utils.diff( const diff =
task_start, date_utils.diff(task_start, gantt_start, this.gantt.config.unit) /
gantt_start, this.gantt.config.step;
this.gantt.config.unit,
);
let x = diff * column_width; let x = diff * column_width;
/* Since the column width is based on 30, /* Since the column width is based on 30,
@ -540,6 +538,13 @@ export default class Bar {
} }
compute_duration() { compute_duration() {
console.log(
date_utils.diff(
this.task._end,
this.task._start,
this.gantt.config.unit,
),
);
this.duration = this.duration =
date_utils.diff( date_utils.diff(
this.task._end, this.task._end,

View File

@ -122,7 +122,7 @@ export default {
return str; return str;
}, },
diff(date_a, date_b, scale = DAY) { diff(date_a, date_b, scale = 'day') {
let milliseconds, seconds, hours, minutes, days, months, years; let milliseconds, seconds, hours, minutes, days, months, years;
milliseconds = date_a - date_b; milliseconds = date_a - date_b;
@ -131,13 +131,14 @@ export default {
hours = minutes / 60; hours = minutes / 60;
days = hours / 24; days = hours / 24;
// Calculate months across years // Calculate months across years
const yearDiff = date_a.getFullYear() - date_b.getFullYear(); let yearDiff = date_a.getFullYear() - date_b.getFullYear();
const monthDiff = date_a.getMonth() - date_b.getMonth(); let monthDiff = date_a.getMonth() - date_b.getMonth();
// calculate extra
monthDiff += (days % 30) / 30;
/* If monthDiff is negative, date_b is in an earlier month than /* If monthDiff is negative, date_b is in an earlier month than
date_a and thus subtracted from the year difference in months */ date_a and thus subtracted from the year difference in months */
months = yearDiff * 12 + monthDiff; months = yearDiff * 12 + monthDiff;
/* If date_a's (e.g. march 1st) day of the month is smaller than date_b (e.g. february 28th), /* If date_a's (e.g. march 1st) day of the month is smaller than date_b (e.g. february 28th),
adjust the month difference */ adjust the month difference */
if (date_a.getDate() < date_b.getDate()) { if (date_a.getDate() < date_b.getDate()) {
@ -151,16 +152,18 @@ export default {
scale += 's'; scale += 's';
} }
return Math.floor( return (
{ Math.round(
milliseconds, {
seconds, milliseconds,
minutes, seconds,
hours, minutes,
days, hours,
months, days,
years, months,
}[scale], years,
}[scale] * 100,
) / 100
); );
}, },

View File

@ -349,6 +349,7 @@ export default class Gantt {
class: 'grid-row', class: 'grid-row',
append_to: rows_layer, append_to: rows_layer,
}); });
// FIX
if ( if (
this.options.lines === 'both' || this.options.lines === 'both' ||
this.options.lines === 'horizontal' this.options.lines === 'horizontal'
@ -750,7 +751,7 @@ export default class Gantt {
? date_utils.format(date, upper_text, this.options.language) ? date_utils.format(date, upper_text, this.options.language)
: upper_text(date, last_date, this.options.language), : upper_text(date, last_date, this.options.language),
lower_text: lower_text:
typeof upper_text === 'string' typeof lower_text === 'string'
? date_utils.format(date, lower_text, this.options.language) ? date_utils.format(date, lower_text, this.options.language)
: lower_text(date, last_date, this.options.language), : lower_text(date, last_date, this.options.language),
upper_x: base_pos.x + x_pos[`${this.config.view_mode.name}_upper`], upper_x: base_pos.x + x_pos[`${this.config.view_mode.name}_upper`],