GithubHelp home page GithubHelp logo

Comments (4)

jdelStrother avatar jdelStrother commented on May 10, 2024 1

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.

JEK58 avatar JEK58 commented on May 10, 2024

Did you come up with a solution? Would be interested how you did manage to get it work.

from chartjs-adapter-date-fns.

jledentu avatar jledentu commented on May 10, 2024

@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.

jmtimko5 avatar jmtimko5 commented on May 10, 2024
_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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.