Blame view

src/components/date-picker/util.js 3.81 KB
0f677893   梁灏   update DatePicker
1
2
  import dateUtil from '../../utils/date';
  
0f677893   梁灏   update DatePicker
3
  export const toDate = function(date) {
58ff14d7   Sergio Crisostomo   Make date more IE...
4
5
6
7
8
9
10
11
12
13
14
      let _date = new Date(date);
      // IE patch start (#1422)
      if (isNaN(_date.getTime()) && typeof date === 'string'){
          _date = date.split('-').map(Number);
          _date[1] += 1;
          _date = new Date(..._date);
      }
      // IE patch end
  
      if (isNaN(_date.getTime())) return null;
      return _date;
0f677893   梁灏   update DatePicker
15
16
17
18
19
20
21
22
23
24
25
26
27
  };
  
  export const formatDate = function(date, format) {
      date = toDate(date);
      if (!date) return '';
      return dateUtil.format(date, format || 'yyyy-MM-dd');
  };
  
  export const parseDate = function(string, format) {
      return dateUtil.parse(string, format || 'yyyy-MM-dd');
  };
  
  export const getDayCountOfMonth = function(year, month) {
c91c30cc   Sergio Crisostomo   Date utils improv...
28
      return new Date(year, month + 1, 0).getDate();
0f677893   梁灏   update DatePicker
29
30
31
32
33
34
35
36
  };
  
  export const getFirstDayOfMonth = function(date) {
      const temp = new Date(date.getTime());
      temp.setDate(1);
      return temp.getDay();
  };
  
c91c30cc   Sergio Crisostomo   Date utils improv...
37
38
39
40
41
42
  export const siblingMonth = function(src, diff) {
      const temp = new Date(src); // lets copy it so we don't change the original
      const newMonth = temp.getMonth() + diff;
      const newMonthDayCount = getDayCountOfMonth(temp.getFullYear(), newMonth);
      if (newMonthDayCount < temp.getDate()) {
          temp.setDate(newMonthDayCount);
0f677893   梁灏   update DatePicker
43
      }
c91c30cc   Sergio Crisostomo   Date utils improv...
44
      temp.setMonth(newMonth);
0f677893   梁灏   update DatePicker
45
  
c91c30cc   Sergio Crisostomo   Date utils improv...
46
47
      return temp;
  };
0f677893   梁灏   update DatePicker
48
  
c91c30cc   Sergio Crisostomo   Date utils improv...
49
50
  export const prevMonth = function(src) {
      return siblingMonth(src, -1);
0f677893   梁灏   update DatePicker
51
52
53
  };
  
  export const nextMonth = function(src) {
c91c30cc   Sergio Crisostomo   Date utils improv...
54
      return siblingMonth(src, 1);
2dbbd7de   梁灏   update TimePicker
55
56
  };
  
c91c30cc   Sergio Crisostomo   Date utils improv...
57
  export const initTimeDate = function() {
2dbbd7de   梁灏   update TimePicker
58
59
60
61
62
      const date = new Date();
      date.setHours(0);
      date.setMinutes(0);
      date.setSeconds(0);
      return date;
c91c30cc   Sergio Crisostomo   Date utils improv...
63
  };
b27858dd   Sergio Crisostomo   add date panel la...
64
65
66
67
68
69
  
  export const formatDateLabels = (function() {
      /*
        Formats:
        yyyy - 4 digit year
        m - month, numeric, 1 - 12
3ed12b4e   Sergio Crisostomo   Correct month cal...
70
        mm - month, numeric, 01 - 12
b27858dd   Sergio Crisostomo   add date panel la...
71
72
73
74
75
76
77
78
        mmm - month, 3 letters, as in `toLocaleDateString`
        Mmm - month, 3 letters, capitalize the return from `toLocaleDateString`
        mmmm - month, full name, as in `toLocaleDateString`
        Mmmm - month, full name, capitalize the return from `toLocaleDateString`
      */
  
      const formats = {
          yyyy: date => date.getFullYear(),
3ed12b4e   Sergio Crisostomo   Correct month cal...
79
80
          m: date => date.getMonth() + 1,
          mm: date => ('0' + (date.getMonth() + 1)).slice(-2),
b27858dd   Sergio Crisostomo   add date panel la...
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
          mmm: (date, locale) => {
              const monthName = date.toLocaleDateString(locale, {
                  month: 'long'
              });
              return monthName.slice(0, 3);
          },
          Mmm: (date, locale) => {
              const monthName = date.toLocaleDateString(locale, {
                  month: 'long'
              });
              return (monthName[0].toUpperCase() + monthName.slice(1).toLowerCase()).slice(0, 3);
          },
          mmmm: (date, locale) =>
              date.toLocaleDateString(locale, {
                  month: 'long'
              }),
          Mmmm: (date, locale) => {
              const monthName = date.toLocaleDateString(locale, {
                  month: 'long'
              });
              return monthName[0].toUpperCase() + monthName.slice(1).toLowerCase();
          }
      };
      const formatRegex = new RegExp(['yyyy', 'Mmmm', 'mmmm', 'Mmm', 'mmm', 'mm', 'm'].join('|'), 'g');
  
      return function(locale, format, date) {
          const componetsRegex = /(\[[^\]]+\])([^\[\]]+)(\[[^\]]+\])/;
          const components = format.match(componetsRegex).slice(1);
          const separator = components[1];
          const labels = [components[0], components[2]].map(component => {
              const label = component.replace(/\[[^\]]+\]/, str => {
                  return str.slice(1, -1).replace(formatRegex, match => formats[match](date, locale));
              });
              return {
                  label: label,
236e0bfd   Sergio Crisostomo   DOn't use include...
116
                  type: component.indexOf('yy') != -1 ? 'year' : 'month'
b27858dd   Sergio Crisostomo   add date panel la...
117
118
119
120
121
122
123
124
              };
          });
          return {
              separator: separator,
              labels: labels
          };
      };
  })();