new Time( [options])
The Time class. Time settings are applied in general for each page using
Highcharts.setOptions, or individually for each Chart item through the
time options set.
The Time object is available from Highcharts.Chart#time, which refers
to Highcharts.time unless individual time settings are applied for each
chart.
When configuring time settings for individual chart instances, be aware that
using Highcharts.dateFormat or Highcharts.time.dateFormat within
formatter callbacks relies on the global time object, which applies the
global language and time zone settings. To ensure charts with local time
settings function correctly, use chart.time.dateFormat? instead. However, the recommended best practice is to use setOptions` to define global time
settings unless specific configurations are needed for each chart.
Parameters:
| Name | Type | Argument | Description |
|---|---|---|---|
options |
Highcharts.TimeOptions |
<optional> |
Time options as defined in chart.options.time. |
- Since:
-
- 6.0.5
Example
// Apply time settings globally
Highcharts.setOptions({
time: {
timezone: 'Europe/London'
}
});
// Apply time settings by instance
const chart = Highcharts.chart('container', {
time: {
timezone: 'America/New_York'
},
series: [{
data: [1, 4, 3, 5]
}]
});
// Use the Time object of a chart instance
console.log(
'Current time in New York',
chart.time.dateFormat('%Y-%m-%d %H:%M:%S', Date.now())
);
// Standalone use
const time = new Highcharts.Time({
timezone: 'America/New_York'
});
const s = time.dateFormat('%Y-%m-%d %H:%M:%S', Date.UTC(2020, 0, 1));
console.log(s); // => 2019-12-31 19:00:00
Methods
-
dateFormat(format [, timestamp] [, upperCaseFirst])
-
Formats a JavaScript date timestamp (milliseconds since January 1 1970) into a human readable date string.
The
formatparameter accepts two types of values:- An object containing settings that are passed directly on to Intl.DateTimeFormat.prototype.format.
- A format string containing either individual or locale-aware format
keys. Individual keys, for example
%Y-%m-%d, are listed below. Locale-aware keys are grouped by square brackets, for example%[Ymd]. The order of keys within the square bracket doesn't affect the output, which is determined by the locale. See example below. Internally, the locale-aware format keys are just a shorthand for the full object formats, but are particularly practical in templating where full object definitions are not an option.
The available string format keys are listed below. Additional formats can be given in the Highcharts.dateFormats hook.
Supported format keys:
Key Description Notes on locale-aware format %ALong weekday, like 'Monday' %aShort weekday, like 'Mon' %ENarrow weekday, single character %dTwo digit day of the month, 01 to 31 %eDay of the month, 1 through 31 %wDay of the week, 0 through 6 N/A %bShort month, like 'Jan' %BLong month, like 'January' %mTwo digit month number, 01 through 12 %oMonth number, 1 through 12 %yTwo digits year, like 24 for 2024 %YFour digits year, like 2024 %HTwo digits hours in 24h format, 00 through 23 Depending on the locale, 12h format may be instered. %kHours in 24h format, 0 through 23 Depending on the locale, 12h format may be instered. %ITwo digits hours in 12h format, 00 through 11 N/A. The locale determines the hour format. %lHours in 12h format, 1 through 12 N/A. The locale determines the hour format. %MTwo digits minutes, 00 through 59 %pUpper case AM or PM N/A. The locale determines whether to add AM and PM. %PLower case AM or PM N/A. The locale determines whether to add AM and PM. %STwo digits seconds, 00 through 59 %LMilliseconds (naming from Ruby) Parameters:
Name Type Argument Default Description formatstring | Highcharts.DateTimeFormatOptions The desired string format where various time representations are prefixed with %, or an object representing the locale-aware format options.
timestampnumber <optional>
The JavaScript timestamp.
upperCaseFirstboolean <optional>
false Upper case first letter in the return.
Returns:
string .The formatted date.
Example
// Object format, US English const time1 = new Highcharts.Time({ locale: 'en-US' }); console.log( time1.dateFormat({ day: 'numeric', month: 'short', year: 'numeric', hour: 'numeric', minute: 'numeric' }, Date.UTC(2024, 11, 31)) ); // => Dec 31, 2024, 12:00 AM // Object format, British English const time2 = new Highcharts.Time({ locale: 'en-GB' }); console.log( time2.dateFormat({ day: 'numeric', month: 'short', year: 'numeric', hour: 'numeric', minute: 'numeric' }, Date.UTC(2024, 11, 31)) ); // => 31 Dec 2024, 00:00 // Individual key string replacement const time3 = new Highcharts.Time(); console.log( time3.dateFormat('%Y-%m-%d %H:%M:%S', Date.UTC(2024, 11, 31)) ); // => 2024-12-31 00:00:00 // Locale-aware keys, US English const time4 = new Highcharts.Time({ locale: 'en-US' }); console.log( time4.dateFormat('%[YebHM]', Date.UTC(2024, 11, 31)) ); // => Dec 31, 2024, 12:00 AM // Locale-aware keys, British English const time5 = new Highcharts.Time({ locale: 'en-GB' }); console.log( time5.dateFormat('%[YebHM]', Date.UTC(2024, 11, 31)) ); // => 31 Dec 2024, 00:00 // Mixed locale-aware and individual keys console.log( time5.dateFormat('%[Yeb], %H:%M', Date.UTC(2024, 11, 31)) ); // => 31 Dec 2024, 00:00 -
getTimeTicks(normalizedInterval [, min] [, max] [, startOfWeek])
-
Return an array with time positions distributed on round time values right and right after min and max. Used in datetime axes as well as for grouping data on a datetime axis.
Parameters:
Name Type Argument Default Description normalizedIntervalHighcharts.TimeNormalizedObject The interval in axis values (ms) and the count
minnumber <optional>
The minimum in axis values
maxnumber <optional>
The maximum in axis values
startOfWeeknumber <optional>
1 Returns:
Highcharts.AxisTickPositionsArray .Time positions
-
getTimezoneOffset(timestamp)
-
Get the time zone offset based on the current timezone information as set in the global options.
Parameters:
Name Type Description timestampnumber The JavaScript timestamp to inspect.
Returns:
number .The timezone offset in minutes compared to UTC.
-
makeTime(year, month [, date] [, hours] [, minutes] [, seconds])
-
Make a time and returns milliseconds. Similar to
Date.UTC, but takes the currenttimezonesetting into account.Parameters:
Name Type Argument Default Description yearnumber The year
monthnumber The month. Zero-based, so January is 0.
datenumber <optional>
1 The day of the month
hoursnumber <optional>
0 The hour of the day, 0-23.
minutesnumber <optional>
0 The minutes
secondsnumber <optional>
0 The seconds
Returns:
number .The time in milliseconds since January 1st 1970.
-
parse(s)
-
Parse a datetime string. Unless the string contains time zone information, apply the current
timezonefrom options. If the argument is a number, return it.Parameters:
Name Type Description sstring | number | undefined The datetime string to parse
Returns:
number | undefined .Parsed JavaScript timestamp
-
toParts( [timestamp])
-
Get a date in terms of numbers (year, month, day etc) for further processing. Takes the current
timezonesetting into account. Inverse ofmakeTimeand the nativeDateconstructor andDate.UTC.The date is returned in array format with the following indices:
0: year, 1: month (zero based), 2: day, 3: hours, 4: minutes, 5: seconds, 6: milliseconds, 7: weekday (Sunday as 0)
Parameters:
Name Type Argument Description timestampnumber | Date <optional>
The timestamp in milliseconds since January 1st 1970. A Date object is also accepted.
Returns:
Array.<number> .The date parts in array format.