Hamed
Hamed

Reputation: 23

Format highcharts date tooltip to display timezone

I want to display the time zone on the date/time tooltip.

The xDateFormat option does not have an option to display timezone.

https://jsfiddle.net/u7z28fbp/1/

For eg:

Currently the date tooltip shows: Friday, April 5, 2019

I want it to show: Friday, April 5, 2019 UTC +00:00

Upvotes: 2

Views: 1813

Answers (2)

Don M
Don M

Reputation: 986

Try this:

https://jsfiddle.net/q8s2jdgp/1/

Highcharts.chart('container', {

xAxis: {
    type: 'datetime'
},

tooltip: {
    xDateFormat: '%A, %B %d, %Y UTC ' + dayjs().format("Z") ,
    shared: true
},

plotOptions: {
    series: {
        pointStart: Date.UTC(2012, 0, 1),
        pointInterval: 24 * 3600 * 1000
    }
},

series: [{
    data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}, {
    data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4].reverse()
}]
});

If you want to change what the UTC value is to the right you will want to write some code around that to create a variable and use concatenation or interpolation.

I used day.js, here are other ways you can get the timestamp from the browser. get client time zone from browser

There are a few resources around to do datetime formatting, I used this one: https://thoughtbot.com/blog/how-to-find-a-time-format-for-rubys-strftime

Upvotes: 1

ppotaczek
ppotaczek

Reputation: 39149

You can add an additional date format specifier to Highcharts.dateFormats object:

Highcharts.dateFormats = {
    'Z': function(time) {
        return new Date(time).getTimezoneOffset() / 60
    }
}


Highcharts.stockChart('container', {
    ...,
    tooltip: {
        xDateFormat: '%A, %B %d, %Y UTC %Z'
    }
});

Live demo: https://jsfiddle.net/BlackLabel/kt7bucjv/

API Reference: https://api.highcharts.com/class-reference/Highcharts.html#.dateFormats

Upvotes: 1

Related Questions