Comments (4)
For what it's worth, I came up with this hack to present all my dates in UTC regardless of user-timezone:
import { Chart, _adapters } from "chart.js";
import "chartjs-adapter-date-fns";
import locale from "lib/browser_locale";
const Formats: Record<string, Intl.DateTimeFormatOptions> = {
"MMM d": {
month: "short",
day: "2-digit",
timeZone: "UTC",
},
PP: {
year: "numeric",
month: "short",
day: "2-digit",
timeZone: "UTC",
},
};
_adapters._date.override({
format: function (time, fmt) {
let utcTime = new Date(time);
let format = Formats[fmt];
if (!format) console.warn(`Missing datetime format for ${fmt}`);
return utcTime.toLocaleDateString(locale, format || { timeZone: "UTC" });
},
});
Here, "MMM d" is chart.js's default format for a date, and "PP" is my format for scales.x.time.tooltipFormat. If you're using other format strings, you'd probably need to figure out your own equivalents.
from chartjs-adapter-date-fns.
Did you come up with a solution? Would be interested how you did manage to get it work.
from chartjs-adapter-date-fns.
@JEK58 No, sorry I didn't found time to work on this. :( I think that the library should handle a timezone option natively
from chartjs-adapter-date-fns.
_adapters._date.override({ format: function (time, fmt) { let utcTime = new Date(time); let format = Formats[fmt]; if (!format) console.warn(`Missing datetime format for ${fmt}`); return utcTime.toLocaleDateString(locale, format || { timeZone: "UTC" }); }, });
This worked great for me as well. Thanks for the solution. The date-fns format function is quite simple:
format: function(time, fmt) {
return format(time, fmt, this.options);
},
I think we should be fine to override it in this manner
from chartjs-adapter-date-fns.
Related Issues (20)
- Fix startOf function for isoWeek HOT 1
- New npm release? HOT 1
- Via Webpack
- Will you update NPM? HOT 1
- fails to resolve toDate if [email protected] is used elsewhere in project HOT 3
- Failed to use with Chart.js 3.0.2 and time x axis HOT 2
- First tick label is not being rendered HOT 5
- Wrong chartjs version in readme. HOT 2
- Importing adapter to typescript file HOT 3
- How to configure locale with bundled (CDN) chartjs-adapter-date-fns.bundle.min.js? HOT 1
- Can we push a new release?
- Don't require "Chart.js"
- Add cdnjs link in readme HOT 2
- Update dependency chart.js@"^4.0.0" HOT 6
- Uncaught TypeError: Cannot read properties of undefined (reading '_adapters') HOT 14
- `No "exports" main defined` error with Vitest
- Error: No "exports" main defined HOT 1
- Error [ERR_REQUIRE_ESM] HOT 1
- chart.min.js:13 Uncaught SyntaxError: Cannot use import statement outside a module (at chart.min.js:13:1) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chartjs-adapter-date-fns.