Blame view

src/components/date-picker/util.js 1.39 KB
0f677893   梁灏   update DatePicker
1
2
  import dateUtil from '../../utils/date';
  
0f677893   梁灏   update DatePicker
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  export const toDate = function(date) {
      date = new Date(date);
      if (isNaN(date.getTime())) return null;
      return date;
  };
  
  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...
20
      return new Date(year, month + 1, 0).getDate();
0f677893   梁灏   update DatePicker
21
22
23
24
25
26
27
28
  };
  
  export const getFirstDayOfMonth = function(date) {
      const temp = new Date(date.getTime());
      temp.setDate(1);
      return temp.getDay();
  };
  
c91c30cc   Sergio Crisostomo   Date utils improv...
29
30
31
32
33
34
  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
35
      }
c91c30cc   Sergio Crisostomo   Date utils improv...
36
      temp.setMonth(newMonth);
0f677893   梁灏   update DatePicker
37
  
c91c30cc   Sergio Crisostomo   Date utils improv...
38
39
      return temp;
  };
0f677893   梁灏   update DatePicker
40
  
c91c30cc   Sergio Crisostomo   Date utils improv...
41
42
  export const prevMonth = function(src) {
      return siblingMonth(src, -1);
0f677893   梁灏   update DatePicker
43
44
45
  };
  
  export const nextMonth = function(src) {
c91c30cc   Sergio Crisostomo   Date utils improv...
46
      return siblingMonth(src, 1);
2dbbd7de   梁灏   update TimePicker
47
48
  };
  
c91c30cc   Sergio Crisostomo   Date utils improv...
49
  export const initTimeDate = function() {
2dbbd7de   梁灏   update TimePicker
50
51
52
53
54
      const date = new Date();
      date.setHours(0);
      date.setMinutes(0);
      date.setSeconds(0);
      return date;
c91c30cc   Sergio Crisostomo   Date utils improv...
55
  };