Skip to main content

date

Description

A set of date formatting methods

date: DateHelpers

Methods

add(date, number, unit)

Adds/subtracts the specified time interval to/from the date

Parameters:

  • date - (Date) - The date object
  • number - (number) - Number of units to add (positive) or subtract (negative)
  • unit - (string) - Time unit: minute, hour, day, week, month, year

Returns: Date - The new date object

Example:

// adds 1 year to the specified date: 29 June, 2027 -> 29 June, 2028
const newDate = gantt.date.add(new Date(2027, 5, 29), 1, 'year');

add_quarter(date, number)

Adds/subtracts the specified number of quarters to/from the date

Parameters:

  • date - (Date) - The date object
  • number - (number) - Number of quarters to add (positive) or subtract (negative)

Returns: Date - The new date object

Example:

// adds 1 quarter (3 months) to the specified date: 
// 29 June, 2027 -> 29 September, 2027
const newDate = gantt.date.add_quarter(new Date(2027, 5, 29), 1);

convert_to_utc(date)

Converts local time to UTC

Parameters:

  • date - (Date) - The date object to convert

Returns: Date - The UTC date object

Example:

// 29 June, 2027 14:00 (local time) -> 29 June, 2027 12:00 (utc)
const utcTime = gantt.date.convert_to_utc(new Date(2027, 5, 29, 14, 0));

copy(date)

Makes a copy of a Date object

Parameters:

  • date - (Date) - The date object to copy

Returns: Date - The copied date object

Example:

const copiedDate = gantt.date.copy(new Date(2027, 5, 29)); // -> 29 June, 2027

date_part(date)

Returns a copy of the date with the time part reset to 00:00:00

Parameters:

  • date - (Date) - The date object to format

Returns: Date - The date with time reset to 00:00:00

Example:

// 29 June, 2027 14:30:10 -> 29 June, 2027 00:00:00
const dateWithoutTime = gantt.date.date_part(new Date(2027, 5, 29, 14, 30, 10));

date_to_str(format, utc)

Returns a function that converts a Date object to a string of the specified format

Parameters:

Returns: Function - The formatting function

Example:

const formatDate = gantt.date.date_to_str("%d/%m/%Y");
const formattedDate = formatDate(new Date(2027, 5, 29)); // -> "29/06/2027"

day_start(date)

Returns a copy of the date with the time part reset to 00:00:00 (alias of date_part())

Parameters:

  • date - (Date) - The date object to format

Returns: Date - The date with time reset to 00:00:00

Example:

// 29 June, 2027 14:30:10 -> 29 June, 2027 00:00:00
const dayStart = gantt.date.day_start(new Date(2027, 5, 29, 14, 30, 10));

getISOWeek(date)

Returns the ISO-8601 week number of the date (weeks start on Monday)

Parameters:

  • date - (Date) - The date object

Returns: number - The week number

Example:

const isoWeek = gantt.date.getISOWeek(new Date(2027, 5, 29)); // ->26

getUTCISOWeek(date)

Returns the week number of the date after converting to UTC

Parameters:

  • date - (Date) - The date object

Returns: number - The week number

Example:

const utcIsoWeek = gantt.date.getUTCISOWeek(new Date(2027, 5, 29)); // ->26

getWeek(date)

Returns the week number of the date (week start depends on gantt.config.start_on_monday)

Parameters:

  • date - (Date) - The date object

Returns: number - The week number

Example:

// weeks start on Sunday
gantt.config.start_on_monday = false;

const isoWeek = gantt.date.getISOWeek(new Date(2027, 2, 25)); // ->12
const week = gantt.date.getWeek(new Date(2027, 2, 25)); // ->13

month_start(date)

Returns the first day of the month with time reset to 00:00:00

Parameters:

  • date - (Date) - The date object

Returns: Date - The first day of month

Example:

// 29 June, 2027 14:30 -> 01 June, 2027 00:00
const firstDayOfMonth = gantt.date.month_start(new Date(2027, 5, 29, 14, 30));

parseDate(date, format)

Converts a date string to a Date object. This method is called during load() and parse() to parse task and link date properties.

Parameters:

  • date - (string) - The date string to parse
  • format - (string | function, optional) - A date format string (see Date Format Specification) or a custom parser function (dateStr) => Date

Returns: Date - The parsed date object

Parsing logic (since v9.1.3):

  1. ISO 8601 check - if the string matches an ISO 8601 pattern (e.g. "2027-01-06", "2027-01-06T10:30:00Z"), it is parsed directly and format is not consulted. If the user has explicitly overridden the parse_date template, ISO auto-detection is skipped and the user's function handles all parsing.
  2. format argument - if provided as a string, it is converted to a parser function via str_to_date(); if provided as a function, it is called directly
  3. Fallback - if no format is provided, the parse_date template is used

Examples:

// with an explicit format string
const parsedDate = gantt.date.parseDate("29/06/2027", "%d/%m/%Y");
// -> 29 June, 2027 00:00:00

// ISO string - parsed automatically, format is ignored
const parsedIsoDate = gantt.date.parseDate("2027-01-06T10:30:00Z");
// -> 6 January, 2027 10:30:00 UTC

// with a custom parser function
const parsedCustomDate = gantt.date.parseDate("Jan 6, 2027", (str) => {
return new Date(str);
});

str_to_date(format, utc)

Returns a function that converts a string to a Date object

Parameters:

Returns: Function - The parsing function

Example:

const parseDate = gantt.date.str_to_date("%d/%m/%Y");
const parsedDate = parseDate("29/06/2027"); // -> 29 June, 2027 00:00:00

time_part(date)

Returns the time as seconds since midnight

Parameters:

  • date - (Date) - The date object

Returns: number - Seconds since midnight

Example:

const secondsSinceMidnight = gantt.date.time_part(new Date(2027, 5, 29, 14, 30, 10));

to_fixed(num)

Adds leading zero to numbers < 10

Parameters:

  • num - (number) - The number to format

Returns: string - The formatted string

Example:

const paddedNumber = gantt.date.to_fixed(2); // ->"02"
const unchangedNumber = gantt.date.to_fixed(10); // ->10

minute_start(date)

Returns a copy of the date with the seconds and milliseconds reset to 00

Parameters:

  • date - (Date) - The date object

Returns: Date - The formatted date

Example:

// 29 June, 2027 14:30:10 -> 29 June, 2027 14:30:00
const minuteStart = gantt.date.minute_start(new Date(2027, 5, 29, 14, 30, 10));

hour_start(date)

Returns a copy of the date with the minutes and seconds reset to 00

Parameters:

  • date - (Date) - The date object

Returns: Date - The formatted date

Example:

// 29 June, 2027 14:30:10 -> 29 June, 2027 14:00:00
const hourStart = gantt.date.hour_start(new Date(2027, 5, 29, 14, 30, 10));

week_start(date)

Returns the first day of the week with time reset to 00:00:00

Parameters:

  • date - (Date) - The date object

Returns: Date - The first day of week

Example:

// 29 June, 2027 14:30 -> 28 June, 2027 00:00
const weekStart = gantt.date.week_start(new Date(2027, 5, 29, 14, 30));

quarter_start(date)

Returns the first month of the quarter with time reset to 00:00:00

Parameters:

  • date - (Date) - The date object

Returns: Date - The first day of quarter

Example:

// 29 June, 2027 14:30:10 -> 01 April, 2027 00:00:00
const quarterStart = gantt.date.quarter_start(new Date(2027, 5, 29, 14, 30, 10));

year_start(date)

Returns the first day of the year with time reset to 00:00:00

Parameters:

  • date - (Date) - The date object

Returns: Date - The first day of year

Example:

// 29 June, 2027 14:30 -> 01 January, 2027 00:00
const yearStart = gantt.date.year_start(new Date(2027, 5, 29, 14, 30));
Need help?
Got a question about the documentation? Reach out to our technical support team for help and guidance. For custom component solutions, visit the Services page.