• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SkylerHu / antd-restful / #40

03 Sep 2025 02:22PM UTC coverage: 80.378% (-0.7%) from 81.07%
#40

push

web-flow
fix: 修复时间选择组件问题

Co-authored-by: hu.leping <hu.leping@hellogroup.com>

1217 of 1600 branches covered (76.06%)

Branch coverage included in aggregate %.

30 of 48 new or added lines in 4 files covered. (62.5%)

3 existing lines in 2 files now uncovered.

1593 of 1896 relevant lines covered (84.02%)

37.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.7
/src/common/dateUtils.js
1
/**
2
 * 根据antd版本自动选择时间库并创建日期对象
3
 * antd4使用moment,antd5+使用dayjs
4
 */
5
import { version as antdVersion } from "antd";
6
import { isString } from "src/common/typeTools";
7

8
let moment = null;
4✔
9
let dayjs = null;
4✔
10

11
// 检测antd版本
12
export function detectAntdVersion() {
13
  const majorVersion = parseInt(antdVersion.split(".")[0]);
148✔
14
  return majorVersion;
148✔
15
}
16

17
// 懒加载moment
18
function getMoment() {
NEW
19
  if (moment === null) {
×
NEW
20
    try {
×
NEW
21
      moment = require("moment");
×
22
    } catch (e) {
23
      // moment is not installed, please install it for antd4 compatibility
NEW
24
      return null;
×
25
    }
26
  }
NEW
27
  return moment;
×
28
}
29

30
// 懒加载dayjs
31
function getDayjs() {
32
  if (dayjs === null) {
140✔
33
    try {
2✔
34
      dayjs = require("dayjs");
2✔
35
    } catch (e) {
36
      // dayjs is not installed, please install it for antd5 compatibility
NEW
37
      return null;
×
38
    }
39
  }
40
  return dayjs;
140✔
41
}
42

43
/**
44
 * 创建日期对象
45
 * @param {string} dateInput - 日期字符串输入
46
 * @param {string} format - 日期格式(仅在使用moment时有效)
47
 * @returns {object|null} 返回对应的日期对象或null
48
 */
49
export function createDate(dateInput, format = null) {
104✔
50
  if (!isString(dateInput)) {
147✔
51
    return null;
7✔
52
  }
53

54
  const version = detectAntdVersion();
140✔
55

56
  if (version >= 5) {
140!
57
    // antd5+ 使用dayjs
58
    const dayjsInstance = getDayjs();
140✔
59
    if (!dayjsInstance) {
140!
60
      // dayjs is required for antd5+
NEW
61
      return null;
×
62
    }
63

64
    try {
140✔
65
      return dayjsInstance(dateInput);
140✔
66
    } catch (e) {
67
      // Failed to parse date with dayjs
NEW
68
      return null;
×
69
    }
70
  } else {
71
    // antd4 使用moment
NEW
72
    const momentInstance = getMoment();
×
NEW
73
    if (!momentInstance) {
×
74
      // moment is required for antd4
NEW
75
      return null;
×
76
    }
77

NEW
78
    try {
×
NEW
79
      if (format) {
×
NEW
80
        return momentInstance(dateInput, format);
×
81
      } else {
NEW
82
        return momentInstance(dateInput);
×
83
      }
84
    } catch (e) {
85
      // Failed to parse date with moment
NEW
86
      return null;
×
87
    }
88
  }
89
}
90

91
/**
92
 * 检查是否为有效日期
93
 * @param {string} dateInput - 日期字符串输入
94
 * @param {string} format - 日期格式(仅在使用moment时有效)
95
 * @returns {boolean} 是否为有效日期
96
 */
97
export function isValidDate(dateInput, format = null) {
8✔
98
  const date = createDate(dateInput, format);
10✔
99
  if (!date) return false;
10✔
100

101
  const version = detectAntdVersion();
8✔
102

103
  if (version >= 5) {
8!
104
    return date.isValid();
8✔
105
  } else {
NEW
106
    return date.isValid();
×
107
  }
108
}
109

110
/**
111
 * 格式化日期
112
 * @param {string} dateInput - 日期字符串输入
113
 * @param {string} format - 输出格式
114
 * @param {string} inputFormat - 输入格式(仅在使用moment时有效)
115
 * @returns {string} 格式化后的日期字符串
116
 */
117
export function formatDate(dateInput, format = "YYYY-MM-DD", inputFormat = null) {
28✔
118
  const date = createDate(dateInput, inputFormat);
26✔
119
  if (!date) return "";
26✔
120

121
  try {
24✔
122
    return date.format(format);
24✔
123
  } catch (e) {
124
    // Failed to format date
NEW
125
    return "";
×
126
  }
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc