工作时间计算
默认情况下,dhtmlxGantt 按日历时间计算任务的持续时间。它假设任务的最终持续时间可以包含周末和节假日。
请查阅 Task end date display & Inclusive end dates 文章,了解任务结束日期的格式。
启用工作时间计算
若要提供按工作时间计算任务持续时间的功能,请使用 work_time 选项:
启用在工作时间内计算任务持续时间的模式
gantt.config.work_time = true; // 从计算中移除非工作时间 /*!*/
gantt.config.skip_off_time = true; /*!*/ // 在图表中隐藏非工作时间
gantt.init("gantt_here");
请注意,skip_off_time 配置选项仅在 PRO 版本中可用。
Duration includes only working days
根据 duration_unit 的取值,dhtmlxGantt 会以不同的时间单位来计算任务的持续时间(例如,若 duration_unit = "hour",则持续时间在工作时段内计算)。

小数格式的任务持续时间
此功能仅 在 PRO 版本中可用。
从 v6.3 开始,dhtmlxGantt 允许通过 Duration Formatter 模块,将任务的持续时间以小数格式(如 "2.5 days"、 "0.5 hours"、"3.75 hours")进行表示。
需要记住的要点是,内部 Gantt 总是以整数值存储任务的持续时间。
而提供的模块允许将用户输入的持续时间从一种格式解析为 Gantt 存储的格式(例如,输入 "1.5 hours" 时,Gantt 将存储分钟数 - 90)。此外,存储的值可以转换回可读格式(将 12 小时转换为 "0.5 days")。

持续时间可以表示为小时、天或其他 duration_unit 配置单位的分数,唯独不能表示为分钟。
实现小数格式
若要实现以小数格式显示任务持续时间,请按下列逻辑操作:
- 将 duration_unit 设置为 minute
gantt.config.work_time = true;
gantt.config.duration_unit = "minute"; /*!*/
请注意,您需要将任务持续时间存储为小于在小数格式中显示的单位的单位。简而言之:
-
如果您希望用户能够将持续时间指定为一个小时的分数(例如 "0.5 hours"),需要将 duration_unit 设置为 minute
-
如果您希望用户能够将持续时间指定为一天的分数,需要将 duration_unit 设置为 hour。在这种情况下,用户将能够把任务持续时间输入为 "0.5 day",但由于持续时间以整数小时存储,"0.5 hour" 将被向上取整为 1 小时。
默认情况下,任务日期会对齐到时间刻度。如果您的时间刻度以天为单位,您可能希望在同一天内拖放任务到不同的小时段时禁用它。
要启用此拖放功能,您需要禁用 round_dnd_dates 并为 time_step 设置合适的值。 :: 例如:
// 全局时间步长为 15 分钟,需将持续时间单位设为 "minute"
gantt.config.time_step = 15;
gantt.config.round_dnd_dates = false;
或
// 全局时间步长为 1 小时
// 当持续时间单位设为 "hour" 时,可以使用该值
gantt.config.time_step = 60;
gantt.config.round_dnd_dates = false;
- 为格式化任务持续时间创建 formatter 对象:
// 格式化持续时间
const formatter = gantt.ext.formatters.durationFormatter({
enter: "day",
store: "minute", // duration_unit
format: "day",
hoursPerDay: 8,
hoursPerWeek: 40,
daysPerMonth: 30
});
- 通过在列参数的 template 属性中定义模板函数,将 formatter.format(task.duration) 作为返回值,向 “Duration” 列添加 formatter 对象:
gantt.config.columns = [
{ name: "text", tree: true, width: 170, resize: true, editor: textEditor },
{ name: "start_date", align: "center", resize: true, editor: dateEditor },
{ name: "duration", label: "Duration", resize: true, align: "center",
template: task => formatter.format(task.duration), width: 100 },
{ name: "add", width: 44 }
];
- 通过在列参数的 time 控件中设置 formatter 属性,将 formatter 对象添加到轻量对话框中:
gantt.config.lightbox.sections = [
{ name: "description", map_to: "text", type: "textarea", height: 70, focus: true },
{ name: "time", map_to: "auto", type: "duration", formatter: formatter }
];
- 如果在网格中启用了内联编辑,您还需要通过在 durationEditor 对象的 formatter 属性中添加 formatter 对象:
const durationEditor = {
type: "duration",
map_to: "duration",
formatter: formatter, /*!*/
min: 0,
max: 1000
};
gantt.config.columns = [
{ name: "text", tree: true, width: 170, resize: true },
{ name: "start_date", align: "center", resize: true },
{ name: "duration", label: "Duration", resize: true, align: "center",
template: (task) => formatter.format(task.duration),
editor: durationEditor, width: 100 },
{ name: "add", width: 44 }
];
如果您已经有 Gantt 的任务持续时间以分钟、小时或其他单位存储,也可以使用 Duration Formatter 模块将持续时间以小数格式呈现。
全局设置
设置工作时间
默认工作时间如下:
- 工作日:周一至周五。
- 工作时间:8:00-12:00,13:00-17:00。
要更改默认工作时间,请使用 setWorkTime 方法:
// 更改工作日的工作时间
gantt.setWorkTime({ hours: ["9:00-18:00"] });
// 将所有周五设为休息日
gantt.setWorkTime({ day: 5, hours: false });
// 更改周五和周六的工作时间
gantt.setWorkTime({ day: 5, hours: ["8:00-12:00"] });
gantt.setWorkTime({ day: 6, hours: ["8:00-12:00"] });
// 将特定日期设为工作日
gantt.setWorkTime({ date: new Date(2025, 2, 31) });
// 将特定日期设为休息日
gantt.setWorkTime({ date: new Date(2025, 0, 1), hours: false });
相关示例: Custom working days and time
为夜班设置工作时间
对方法 setWorkTime 的配置对象中 hours 属性的工作时间设置应从较小区间到较大区间,按升序排列。若以降序提供时间设置,部分内容将被忽略。在下面的示例中,18:00 之后的时间段将被忽略:
// 下面的设置不正确
gantt.setWorkTime({ day: 5, hours: ["16:00-18:00", "14:00-15:00", "08:00-10:00"] });
gantt.setWorkTime({ day: 5, hours: ["16:00-18:00", "00:00-04:00", "05:00-06:00"] });
如果需要为夜班指定工作时间,请按以下方式设置:
- 第一天内为 24 小时
- 次日内为 24 小时
例如:
gantt.setWorkTime({ day: 5, hours: ["16:00-18:00"] });
gantt.setWorkTime({ day: 6, hours: ["00:00-04:00", "05:00-06:00"] });