-
Notifications
You must be signed in to change notification settings - Fork 11.9k
/
adapter.moment.js
72 lines (59 loc) · 1.51 KB
/
adapter.moment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// TODO v3 - make this adapter external (chartjs-adapter-moment)
'use strict';
var moment = require('moment');
var adapters = require('../core/core.adapters');
var FORMATS = {
datetime: 'MMM D, YYYY, h:mm:ss a',
millisecond: 'h:mm:ss.SSS a',
second: 'h:mm:ss a',
minute: 'h:mm a',
hour: 'hA',
day: 'MMM D',
week: 'll',
month: 'MMM YYYY',
quarter: '[Q]Q - YYYY',
year: 'YYYY'
};
adapters._date.override(moment ? {
_id: 'moment', // DEBUG ONLY
formats: function() {
return FORMATS;
},
parse: function(value, format) {
if (typeof value === 'string' && typeof format === 'string') {
value = moment(value, format);
} else if (!(value instanceof moment)) {
value = moment(value);
}
return value.isValid() ? value.valueOf() : null;
},
format: function(time, format) {
return moment(time).format(format);
},
add: function(time, amount, unit) {
return moment(time).add(amount, unit).valueOf();
},
diff: function(max, min, unit) {
return moment.duration(moment(max).diff(moment(min))).as(unit);
},
startOf: function(time, unit, weekday) {
time = moment(time);
if (unit === 'isoWeek') {
return time.isoWeekday(weekday).valueOf();
}
return time.startOf(unit).valueOf();
},
endOf: function(time, unit) {
return moment(time).endOf(unit).valueOf();
},
// DEPRECATIONS
/**
* Provided for backward compatibility with scale.getValueForPixel().
* @deprecated since version 2.8.0
* @todo remove at version 3
* @private
*/
_create: function(time) {
return moment(time);
},
} : {});