|
| 1 | +/*! |
| 2 | + * chartjs-adapter-moment v0.1.1 |
| 3 | + * https://www.chartjs.org |
| 4 | + * (c) 2020 Chart.js Contributors |
| 5 | + * Released under the MIT license |
| 6 | + */ |
| 7 | +(function (global, factory) { |
| 8 | +typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('moment'), require('chart.js')) : |
| 9 | +typeof define === 'function' && define.amd ? define(['moment', 'chart.js'], factory) : |
| 10 | +(global = global || self, factory(global.moment, global.Chart)); |
| 11 | +}(this, (function (moment, chart_js) { 'use strict'; |
| 12 | + |
| 13 | +moment = moment && moment.hasOwnProperty('default') ? moment['default'] : moment; |
| 14 | + |
| 15 | +const FORMATS = { |
| 16 | + datetime: 'MMM D, YYYY, h:mm:ss a', |
| 17 | + millisecond: 'h:mm:ss.SSS a', |
| 18 | + second: 'h:mm:ss a', |
| 19 | + minute: 'h:mm a', |
| 20 | + hour: 'hA', |
| 21 | + day: 'MMM D', |
| 22 | + week: 'll', |
| 23 | + month: 'MMM YYYY', |
| 24 | + quarter: '[Q]Q - YYYY', |
| 25 | + year: 'YYYY' |
| 26 | +}; |
| 27 | + |
| 28 | +chart_js._adapters._date.override(typeof moment === 'function' ? { |
| 29 | + _id: 'moment', // DEBUG ONLY |
| 30 | + |
| 31 | + formats: function() { |
| 32 | + return FORMATS; |
| 33 | + }, |
| 34 | + |
| 35 | + parse: function(value, format) { |
| 36 | + if (typeof value === 'string' && typeof format === 'string') { |
| 37 | + value = moment(value, format); |
| 38 | + } else if (!(value instanceof moment)) { |
| 39 | + value = moment(value); |
| 40 | + } |
| 41 | + return value.isValid() ? value.valueOf() : null; |
| 42 | + }, |
| 43 | + |
| 44 | + format: function(time, format) { |
| 45 | + return moment(time).format(format); |
| 46 | + }, |
| 47 | + |
| 48 | + add: function(time, amount, unit) { |
| 49 | + return moment(time).add(amount, unit).valueOf(); |
| 50 | + }, |
| 51 | + |
| 52 | + diff: function(max, min, unit) { |
| 53 | + return moment(max).diff(moment(min), unit); |
| 54 | + }, |
| 55 | + |
| 56 | + startOf: function(time, unit, weekday) { |
| 57 | + time = moment(time); |
| 58 | + if (unit === 'isoWeek') { |
| 59 | + return time.isoWeekday(weekday).valueOf(); |
| 60 | + } |
| 61 | + return time.startOf(unit).valueOf(); |
| 62 | + }, |
| 63 | + |
| 64 | + endOf: function(time, unit) { |
| 65 | + return moment(time).endOf(unit).valueOf(); |
| 66 | + } |
| 67 | +} : {}); |
| 68 | + |
| 69 | +}))); |
0 commit comments