mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-02 03:58:07 +08:00
feat:补充DATETOSTR能力&新增日期范围解析函数
This commit is contained in:
parent
0fb84dd95b
commit
3270eaa51c
@ -200,7 +200,44 @@ test('formula:date', () => {
|
||||
expect(evalFormual('DATETOSTR(NOW(), "YYYY-MM-DD")')).toBe(
|
||||
moment().format('YYYY-MM-DD')
|
||||
);
|
||||
expect(evalFormual('DATETOSTR(1676563200, "YYYY-MM-DD")')).toBe(
|
||||
moment(1676563200, 'X').format('YYYY-MM-DD')
|
||||
);
|
||||
expect(evalFormual('DATETOSTR(1676563200000, "YYYY-MM-DD")')).toBe(
|
||||
moment(1676563200000, 'x').format('YYYY-MM-DD')
|
||||
);
|
||||
expect(evalFormual('DATETOSTR("12/25/2022", "YYYY-MM-DD")')).toBe(
|
||||
moment('12/25/2022').format('YYYY-MM-DD')
|
||||
);
|
||||
expect(evalFormual('YEAR(STRTODATE("2021-10-24 10:10:10"))')).toBe(2021);
|
||||
expect(
|
||||
evalFormual(
|
||||
'DATERANGESPLIT("1676563200,1676735999", undefined, "YYYY.MM.DD hh:mm:ss")'
|
||||
)
|
||||
).toBe('2023.02.17 12:00:00,2023.02.18 11:59:59');
|
||||
expect(evalFormual('DATERANGESPLIT("1676563200,1676735999", 0)')).toBe(
|
||||
'1676563200'
|
||||
);
|
||||
expect(
|
||||
evalFormual(
|
||||
'DATERANGESPLIT("1676563200,1676735999", 0 , "YYYY.MM.DD hh:mm:ss")'
|
||||
)
|
||||
).toBe('2023.02.17 12:00:00');
|
||||
expect(
|
||||
evalFormual(
|
||||
'DATERANGESPLIT("1676563200,1676735999", "start" , "YYYY.MM.DD hh:mm:ss")'
|
||||
)
|
||||
).toBe('2023.02.17 12:00:00');
|
||||
expect(
|
||||
evalFormual(
|
||||
'DATERANGESPLIT("1676563200,1676735999", 1 , "YYYY.MM.DD hh:mm:ss")'
|
||||
)
|
||||
).toBe('2023.02.18 11:59:59');
|
||||
expect(
|
||||
evalFormual(
|
||||
'DATERANGESPLIT("1676563200,1676735999", "end" , "YYYY.MM.DD hh:mm:ss")'
|
||||
)
|
||||
).toBe('2023.02.18 11:59:59');
|
||||
});
|
||||
|
||||
test('formula:last', () => {
|
||||
|
@ -616,12 +616,39 @@
|
||||
|
||||
用法:`DATETOSTR(date, 'YYYY-MM-DD')`
|
||||
|
||||
* `date:date` 日期对象
|
||||
* `date:any` 日期对象、日期字符串、时间戳
|
||||
* `format:string` 日期格式,默认为 "YYYY-MM-DD HH:mm:ss"
|
||||
|
||||
返回:`number` 日期字符串
|
||||
返回:`string` 日期字符串
|
||||
|
||||
将日期转成日期字符串
|
||||
对日期、日期字符串、时间戳进行格式化
|
||||
|
||||
示例:
|
||||
|
||||
DATETOSTR('12/25/2022', 'YYYY-MM-DD') 得到 '2022.12.25'
|
||||
DATETOSTR(1676563200, 'YYYY.MM.DD') 得到 '2023.02.17'
|
||||
DATETOSTR(1676563200000, 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.17 12:00:00'
|
||||
DATETOSTR(DATE('2021-12-21'), 'YYYY.MM.DD hh:mm:ss') 得到 '2021.12.21 08:00:00'
|
||||
|
||||
### DATERANGESPLIT
|
||||
|
||||
用法:`DATERANGESPLIT(date, 'YYYY-MM-DD')`
|
||||
|
||||
* `date:string` 日期范围字符串
|
||||
* `key:string` 取值标识,0或'start'表示获取开始时间,1或'end'表示获取结束时间
|
||||
* `format:string` 日期格式,可选
|
||||
|
||||
返回:`string` 日期字符串
|
||||
|
||||
获取日期范围字符串中的开始时间、结束时间
|
||||
|
||||
示例:
|
||||
|
||||
DATERANGESPLIT('1676563200, 1676735999', undefined , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.17 12:00:00,2023.02.18 11:59:59'
|
||||
DATERANGESPLIT('1676563200, 1676735999', 0 , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.17 12:00:00'
|
||||
DATERANGESPLIT('1676563200, 1676735999', 'start' , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.17 12:00:00'
|
||||
DATERANGESPLIT('1676563200, 1676735999', 1 , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.18 11:59:59'
|
||||
DATERANGESPLIT('1676563200, 1676735999', 'end' , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.18 11:59:59'
|
||||
|
||||
### STARTOF
|
||||
|
||||
|
@ -1031,13 +1031,13 @@ export const doc: {
|
||||
},
|
||||
{
|
||||
name: "DATETOSTR",
|
||||
description: "将日期转成日期字符串",
|
||||
description: "对日期、日期字符串、时间戳进行格式化\n\n示例:\n\nDATETOSTR('12/25/2022', 'YYYY-MM-DD') 得到 '2022.12.25'\nDATETOSTR(1676563200, 'YYYY.MM.DD') 得到 '2023.02.17'\nDATETOSTR(1676563200000, 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.17 12:00:00'\nDATETOSTR(DATE('2021-12-21'), 'YYYY.MM.DD hh:mm:ss') 得到 '2021.12.21 08:00:00'",
|
||||
example: "DATETOSTR(date, 'YYYY-MM-DD')",
|
||||
params: [
|
||||
{
|
||||
type: "date",
|
||||
type: "any",
|
||||
name: "date",
|
||||
description: "日期对象"
|
||||
description: "日期对象、日期字符串、时间戳"
|
||||
},
|
||||
{
|
||||
type: "string",
|
||||
@ -1046,7 +1046,34 @@ export const doc: {
|
||||
}
|
||||
],
|
||||
returns: {
|
||||
type: "number",
|
||||
type: "string",
|
||||
description: "日期字符串"
|
||||
},
|
||||
namespace: "日期函数"
|
||||
},
|
||||
{
|
||||
name: "DATERANGESPLIT",
|
||||
description: "获取日期范围字符串中的开始时间、结束时间\n\n示例:\n\nDATERANGESPLIT('1676563200, 1676735999', undefined , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.17 12:00:00,2023.02.18 11:59:59'\nDATERANGESPLIT('1676563200, 1676735999', 0 , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.17 12:00:00'\nDATERANGESPLIT('1676563200, 1676735999', 'start' , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.17 12:00:00'\nDATERANGESPLIT('1676563200, 1676735999', 1 , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.18 11:59:59'\nDATERANGESPLIT('1676563200, 1676735999', 'end' , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.18 11:59:59'",
|
||||
example: "DATERANGESPLIT(date, 'YYYY-MM-DD')",
|
||||
params: [
|
||||
{
|
||||
type: "string",
|
||||
name: "date",
|
||||
description: "日期范围字符串"
|
||||
},
|
||||
{
|
||||
type: "string",
|
||||
name: "key",
|
||||
description: "取值标识,0或'start'表示获取开始时间,1或'end'表示获取结束时间"
|
||||
},
|
||||
{
|
||||
type: "string",
|
||||
name: "format",
|
||||
description: "日期格式,可选"
|
||||
}
|
||||
],
|
||||
returns: {
|
||||
type: "string",
|
||||
description: "日期字符串"
|
||||
},
|
||||
namespace: "日期函数"
|
||||
|
@ -1498,20 +1498,79 @@ export class Evaluator {
|
||||
}
|
||||
|
||||
/**
|
||||
* 将日期转成日期字符串
|
||||
* 对日期、日期字符串、时间戳进行格式化
|
||||
*
|
||||
* 示例:
|
||||
*
|
||||
* DATETOSTR('12/25/2022', 'YYYY-MM-DD') 得到 '2022.12.25'
|
||||
* DATETOSTR(1676563200, 'YYYY.MM.DD') 得到 '2023.02.17'
|
||||
* DATETOSTR(1676563200000, 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.17 12:00:00'
|
||||
* DATETOSTR(DATE('2021-12-21'), 'YYYY.MM.DD hh:mm:ss') 得到 '2021.12.21 08:00:00'
|
||||
*
|
||||
* @example DATETOSTR(date[, format="YYYY-MM-DD HH:mm:ss"])
|
||||
* @example DATETOSTR(date, 'YYYY-MM-DD')
|
||||
* @namespace 日期函数
|
||||
* @param {date} date 日期对象
|
||||
* @param {any} date 日期对象、日期字符串、时间戳
|
||||
* @param {string} format 日期格式,默认为 "YYYY-MM-DD HH:mm:ss"
|
||||
*
|
||||
* @returns {number} 日期字符串
|
||||
* @returns {string} 日期字符串
|
||||
*/
|
||||
fnDATETOSTR(date: Date, format = 'YYYY-MM-DD HH:mm:ss') {
|
||||
fnDATETOSTR(
|
||||
date: Date | string | number,
|
||||
format: string = 'YYYY-MM-DD HH:mm:ss'
|
||||
) {
|
||||
date = this.normalizeDate(date);
|
||||
return moment(date).format(format);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取日期范围字符串中的开始时间、结束时间
|
||||
*
|
||||
* 示例:
|
||||
*
|
||||
* DATERANGESPLIT('1676563200, 1676735999', undefined , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.17 12:00:00,2023.02.18 11:59:59'
|
||||
* DATERANGESPLIT('1676563200, 1676735999', 0 , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.17 12:00:00'
|
||||
* DATERANGESPLIT('1676563200, 1676735999', 'start' , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.17 12:00:00'
|
||||
* DATERANGESPLIT('1676563200, 1676735999', 1 , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.18 11:59:59'
|
||||
* DATERANGESPLIT('1676563200, 1676735999', 'end' , 'YYYY.MM.DD hh:mm:ss') 得到 '2023.02.18 11:59:59'
|
||||
*
|
||||
* @example DATERANGESPLIT(date, 'YYYY-MM-DD')
|
||||
* @namespace 日期函数
|
||||
* @param {string} date 日期范围字符串
|
||||
* @param {string} key 取值标识,0或'start'表示获取开始时间,1或'end'表示获取结束时间
|
||||
* @param {string} format 日期格式,可选
|
||||
*
|
||||
* @returns {string} 日期字符串
|
||||
*/
|
||||
fnDATERANGESPLIT(daterange: string, key?: string, format?: string) {
|
||||
if (!daterange) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const dateArr = daterange.split(',');
|
||||
let start: any = dateArr[0].trim();
|
||||
let end: any = dateArr[1].trim();
|
||||
|
||||
if (key === undefined || ![0, 1, '0', '1', 'start', 'end'].includes(key)) {
|
||||
if (format && start && end) {
|
||||
start = this.normalizeDate(start);
|
||||
end = this.normalizeDate(end);
|
||||
return `${moment(start).format(format)},${moment(end).format(format)}`;
|
||||
}
|
||||
|
||||
return daterange;
|
||||
}
|
||||
|
||||
if ([0, '0', 'start'].includes(key) && start) {
|
||||
const startDate = this.normalizeDate(start);
|
||||
return format ? moment(startDate).format(format) : start;
|
||||
}
|
||||
|
||||
if ([1, '1', 'end'].includes(key) && end) {
|
||||
const endDate = this.normalizeDate(end);
|
||||
return format ? moment(endDate).format(format) : end;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回日期的指定范围的开端
|
||||
*
|
||||
@ -1542,13 +1601,14 @@ export class Evaluator {
|
||||
}
|
||||
|
||||
normalizeDate(raw: any): Date {
|
||||
if (typeof raw === 'number' || !isNaN(raw)) {
|
||||
return new Date(Number(raw));
|
||||
}
|
||||
|
||||
if (typeof raw === 'string') {
|
||||
const formats = ['', 'YYYY-MM-DD HH:mm:ss', 'X'];
|
||||
if (typeof raw === 'string' || typeof raw === 'number') {
|
||||
let formats = ['', 'YYYY-MM-DD HH:mm:ss', 'X'];
|
||||
|
||||
if (/^\d{10}((\.\d+)*)$/.test(raw.toString())) {
|
||||
formats = ['X', 'x', 'YYYY-MM-DD HH:mm:ss', ''];
|
||||
} else if (/^\d{13}((\.\d+)*)$/.test(raw.toString())) {
|
||||
formats = ['x', 'X', 'YYYY-MM-DD HH:mm:ss', ''];
|
||||
}
|
||||
while (formats.length) {
|
||||
const format = formats.shift()!;
|
||||
const date = moment(raw, format);
|
||||
|
Loading…
Reference in New Issue
Block a user