ECharts number parseDate 格式化时间工具类
ECharts About 4,634 wordsnumber.parseDate
将时间戳、时间字符串、Date类型格式化为Date类型。
echarts.number.parseDate(value: unknown)
使用
echarts.number.parseDate('2025-3')
// Sat Mar 01 2025 00:00:00 GMT+0800 (China Standard Time)
echarts.number.parseDate('2025-3-1')
// Sat Mar 01 2025 00:00:00 GMT+0800 (China Standard Time)
echarts.number.parseDate('2025')
// Wed Jan 01 2025 00:00:00 GMT+0800 (China Standard Time)
echarts.number.parseDate('2012-03-01 05:06')
// Thu Mar 01 2012 05:06:00 GMT+0800 (China Standard Time)
echarts.number.parseDate('2012-03-01T12:22:33-05:00')
// Fri Mar 02 2012 01:22:33 GMT+0800 (China Standard Time)
参数 value
Date类型UTC时间戳ISO 8601子集- 只有年月日的字符串
- '2012-03'
- '2012-03-01'
- '2012-03-01 05'
- '2012-03-01 05:06'
T或空格隔开的字符串- '2012-03-01T12:22:33.123'
- '2012-03-01 12:22:33.123'
- 带时区的字符串
- '2012-03-01T12:22:33Z'
- '2012-03-01T12:22:33+8000'
- '2012-03-01T12:22:33-05:00'
- 只有年月日的字符串
- 其他字符串
- '2012'
- '2012-3'
- '2012-3-1'
- '2012/3/1'
- '2012/03/01'
- '2009/6/12 2:00'
- '2009/6/12 2:05:08'
- '2009/6/12 2:05:08.123'
没有带时区的字符串都将实用本地时区。
不合法的参数将返回new Date(NaN),也是Date对象,但其getFullYear、getTime等方法都返回NaN。
备注
new Date任何时区都会转成浏览器本地时区。
Date本质是UTC时间戳,只有getUTCHours和getUTCXxx方法返回时区信息,其他都是转成本地时区后的信息。
new Date('2025-01-01T00:00:00-15:00')
Wed Jan 01 2025 23:00:00 GMT+0800 (China Standard Time)
new Date('2025-01-01T00:00:00-15:00').getHours();
23
new Date('2025-01-01T00:00:00-15:00').getUTCHours()
15
源码 number.ts
/**
* @param value valid type: number | string | Date, otherwise return `new Date(NaN)`
* These values can be accepted:
* + An instance of Date, represent a time in its own time zone.
* + Or string in a subset of ISO 8601, only including:
* + only year, month, date: '2012-03', '2012-03-01', '2012-03-01 05', '2012-03-01 05:06',
* + separated with T or space: '2012-03-01T12:22:33.123', '2012-03-01 12:22:33.123',
* + time zone: '2012-03-01T12:22:33Z', '2012-03-01T12:22:33+8000', '2012-03-01T12:22:33-05:00',
* all of which will be treated as local time if time zone is not specified
* (see <https://momentjs.com/>).
* + Or other string format, including (all of which will be treated as local time):
* '2012', '2012-3-1', '2012/3/1', '2012/03/01',
* '2009/6/12 2:00', '2009/6/12 2:05:08', '2009/6/12 2:05:08.123'
* + a timestamp, which represent a time in UTC.
* @return date Never be null/undefined. If invalid, return `new Date(NaN)`.
*/
export function parseDate(value: unknown): Date {
if (value instanceof Date) {
return value;
}
else if (zrUtil.isString(value)) {
// Different browsers parse date in different way, so we parse it manually.
// Some other issues:
// new Date('1970-01-01') is UTC,
// new Date('1970/01/01') and new Date('1970-1-01') is local.
// See issue #3623
const match = TIME_REG.exec(value);
if (!match) {
// return Invalid Date.
return new Date(NaN);
}
// Use local time when no timezone offset is specified.
if (!match[8]) {
// match[n] can only be string or undefined.
// But take care of '12' + 1 => '121'.
return new Date(
+match[1],
+(match[2] || 1) - 1,
+match[3] || 1,
+match[4] || 0,
+(match[5] || 0),
+match[6] || 0,
match[7] ? +match[7].substring(0, 3) : 0
);
}
// Timezoneoffset of Javascript Date has considered DST (Daylight Saving Time,
// https://tc39.github.io/ecma262/#sec-daylight-saving-time-adjustment).
// For example, system timezone is set as "Time Zone: America/Toronto",
// then these code will get different result:
// `new Date(1478411999999).getTimezoneOffset(); // get 240`
// `new Date(1478412000000).getTimezoneOffset(); // get 300`
// So we should not use `new Date`, but use `Date.UTC`.
else {
let hour = +match[4] || 0;
if (match[8].toUpperCase() !== 'Z') {
hour -= +match[8].slice(0, 3);
}
return new Date(Date.UTC(
+match[1],
+(match[2] || 1) - 1,
+match[3] || 1,
hour,
+(match[5] || 0),
+match[6] || 0,
match[7] ? +match[7].substring(0, 3) : 0
));
}
}
else if (value == null) {
return new Date(NaN);
}
return new Date(Math.round(value as number));
}
文档
ECharts没有提供关于工具类的官方文档,只能从源码中查看。
工具类位于src/util下。
https://github.com/apache/echarts/blob/master/src/util/number.ts
Views: 5 · Posted: 2026-03-16
———         Thanks for Reading         ———
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...