Get Timezone App Connect Custom component via DayJS

Normally I would have included this via a custom formatter and chain it to an empty string

dmx.Formatter('string', 'userTimezone', function () {
  return dayjs.tz.guess()
});
''.getTimezone()

But I though I would give custom components a try.

  1. Add Dayjs library(and plugins) to your page via CDN or locally.
<script src="https://unpkg.com/dayjs@1.10.4/dayjs.min.js"></script>
<script src="https://unpkg.com/dayjs@1.10.4/plugin/utc.js"></script>
<script src="https://unpkg.com/dayjs@1.10.4/plugin/timezone.js"></script>
  1. Add Custom Component code to your page via script tag
dmx.Component("dayjs", {
  methods: {
    getTimezone: function () {
      return dayjs.tz.guess();
    }
  },
  events: {},
  render: function (t) {
    dayjs.extend(window.dayjs_plugin_utc)
    dayjs.extend(window.dayjs_plugin_timezone)
  }
});
  1. Add html component
<dmx-dayjs id="dayjs1"></dmx-dayjs>
  1. Profit
<dmx-serverconnect id="weekly_steps" url="/api/fitness/getDataWeekly" onsuccess="renderChart()" dmx-param:tz="dayjs1.getTimezone()"></dmx-serverconnect>

Sure this component can be improved with additional methods, events and plugin extension only when needed.

4 Likes