Skip to main content

format_date

Description​

Converts a date object to a date string. Used to send data back to the server

format_date: (date: Date) => string;

Parameters​

  • date - (required) Date - the date which needs formatting

Returns​

  • text - (string) - a text representation of the date

Example​

var dateToStr = gantt.date.date_to_str("%Y-%m-%d %H:%i");
gantt.templates.format_date = function(date){
return dateToStr(date);
};

Details​

Check Date Format Specification.

Loading dates in ISO format​

Since v9.1.3, when ISO 8601 dates are detected on input, dates are serialized back as ISO strings automatically - unless you explicitly override this template. If you define a custom format_date function, it takes priority and is used for all dates, including ISO.

Gantt v9.1.2 and earlier

In versions before v9.1.3, ISO dates were not detected automatically. If you are using an older version, you need to override the templates to handle ISO strings:

gantt.templates.parse_date = function(date) {
return new Date(date);
};
gantt.templates.format_date = function(date) {
return date.toISOString();
};

In v9.1.3+, these overrides are unnecessary for ISO dates.

For more details, see Loading dates in ISO format.

Changing the date format dynamically​

If you need to change the date format dynamically, it is necessary to modify the parse_date template in the following way:

var cfg = gantt.config;
var strToDate = gantt.date.str_to_date(cfg.date_format, cfg.server_utc);

gantt.templates.parse_date = function(date){
return strToDate(date);
};
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.