chartjs-adapter-moment.js (2248B)
1 /*! 2 * chartjs-adapter-moment v1.0.0 3 * https://www.chartjs.org 4 * (c) 2021 chartjs-adapter-moment 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 = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.moment, global.Chart)); 11 }(this, (function (moment, chart_js) { 'use strict'; 12 13 function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } 14 15 var moment__default = /*#__PURE__*/_interopDefaultLegacy(moment); 16 17 const FORMATS = { 18 datetime: 'MMM D, YYYY, h:mm:ss a', 19 millisecond: 'h:mm:ss.SSS a', 20 second: 'h:mm:ss a', 21 minute: 'h:mm a', 22 hour: 'hA', 23 day: 'MMM D', 24 week: 'll', 25 month: 'MMM YYYY', 26 quarter: '[Q]Q - YYYY', 27 year: 'YYYY' 28 }; 29 30 chart_js._adapters._date.override(typeof moment__default['default'] === 'function' ? { 31 _id: 'moment', // DEBUG ONLY 32 33 formats: function() { 34 return FORMATS; 35 }, 36 37 parse: function(value, format) { 38 if (typeof value === 'string' && typeof format === 'string') { 39 value = moment__default['default'](value, format); 40 } else if (!(value instanceof moment__default['default'])) { 41 value = moment__default['default'](value); 42 } 43 return value.isValid() ? value.valueOf() : null; 44 }, 45 46 format: function(time, format) { 47 return moment__default['default'](time).format(format); 48 }, 49 50 add: function(time, amount, unit) { 51 return moment__default['default'](time).add(amount, unit).valueOf(); 52 }, 53 54 diff: function(max, min, unit) { 55 return moment__default['default'](max).diff(moment__default['default'](min), unit); 56 }, 57 58 startOf: function(time, unit, weekday) { 59 time = moment__default['default'](time); 60 if (unit === 'isoWeek') { 61 weekday = Math.trunc(Math.min(Math.max(0, weekday), 6)); 62 return time.isoWeekday(weekday).startOf('day').valueOf(); 63 } 64 return time.startOf(unit).valueOf(); 65 }, 66 67 endOf: function(time, unit) { 68 return moment__default['default'](time).endOf(unit).valueOf(); 69 } 70 } : {}); 71 72 })));