feat:增加DATETOWEEK表达式

This commit is contained in:
lvxiaojiao 2023-02-27 16:58:27 +08:00
parent c5a1b4bd76
commit be6f7ce1b8
4 changed files with 57 additions and 3 deletions

View File

@ -265,6 +265,9 @@ test('formula:date', () => {
'DATERANGESPLIT("1676563200,1676735999", "end" , "YYYY.MM.DD hh:mm:ss")'
)
).toBe('2023.02.18 11:59:59');
expect(evalFormual('DATETOWEEK("2023-02-27")')).toBe(
moment('2023-02-27').day()
);
});
test('formula:last', () => {

View File

@ -652,6 +652,20 @@ DATERANGESPLIT('1676563200, 1676735999', 'start' , 'YYYY.MM.DD hh:mm:ss') 得到
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'
### DATETOWEEK
用法:`DATETOWEEK(date)`
* `date:any` 日期
返回:`number` 星期几的数字标识
获取日期的星期几从0到6分别表示星期日、一、二...六
示例:
DATETOWEEK('2023-02-27') 得到 1
### STARTOF
用法:`STARTOF(date[unit = "day"])`

View File

@ -1083,6 +1083,23 @@ export const doc: {
},
namespace: "日期函数"
},
{
name: "DATETOWEEK",
description: "获取日期的星期几从0到6分别表示星期日、一、二...六\n\n示例\n\nDATETOWEEK('2023-02-27') 得到 1",
example: "DATETOWEEK(date)",
params: [
{
type: "any",
name: "date",
description: "日期"
}
],
returns: {
type: "number",
description: "星期几的数字标识"
},
namespace: "日期函数"
},
{
name: "STARTOF",
description: "返回日期的指定范围的开端",

View File

@ -1470,7 +1470,10 @@ export class Evaluator {
* @returns {number}
*/
fnTIMESTAMP(date: Date, format?: 'x' | 'X') {
return parseInt(moment(date).format(format === 'x' ? 'x' : 'X'), 10);
return parseInt(
moment(this.normalizeDate(date)).format(format === 'x' ? 'x' : 'X'),
10
);
}
/**
@ -1572,6 +1575,23 @@ export class Evaluator {
return dateArr;
}
/**
* 06...
*
*
*
* DATETOWEEK('2023-02-27') 1
*
* @example DATETOWEEK(date)
* @namespace
* @param {any} date
*
* @returns {number}
*/
fnDATETOWEEK(date: Date | string | number) {
return moment(this.normalizeDate(date)).day();
}
/**
*
*
@ -1582,7 +1602,7 @@ export class Evaluator {
* @returns {date}
*/
fnSTARTOF(date: Date, unit?: any) {
return moment(date)
return moment(this.normalizeDate(date))
.startOf(unit || 'day')
.toDate();
}
@ -1596,7 +1616,7 @@ export class Evaluator {
* @returns {date}
*/
fnENDOF(date: Date, unit?: any) {
return moment(date)
return moment(this.normalizeDate(date))
.endOf(unit || 'day')
.toDate();
}