小时总和 nestjs
Sum of hour nestjs
在函数下面的 javascript 代码中,在数据库(使用 typerom)上执行一个 select,其中的数组格式如下:
JSON 数据示例:
{
"id": 1,
"nome": "prova2",
"datacreazione": "2021-09-05T08:41:29.000Z",
"costo": 23,
"ore": "08:00",
"IdPreventivo": 1,
"IdUtente": 2
},
{
"id": 2,
"nome": "prova2",
"datacreazione": "2021-09-06T08:38:26.000Z",
"costo": 23,
"ore": "08:00",
"IdPreventivo": 1,
"IdUtente": 2
}
小时数 (ore
) 字段包含 activity 的总小时数,我想做的是通过添加和执行所有 [=12] 的总和来计算总小时数=] 在该字段中为数组的每个元素输入时间,格式为 HH: MM,我该怎么做?
AttivitaprevService.js
@Injectable()
export class AttivitaprevService {
constructor(
@InjectRepository(Attivitaprev) private repo: Repository<Attivitaprev>
) {}
create(dto: CreateAttivitaprevDto) {
return this.repo.save(dto);
}
findAll() {
return `This action returns all attivitaprev`;
}
async findOne(id: number) {
return await this.repo.find({
where: { IdPreventivo: id },
});
}
sumofhour = (time1, time2) => {
let [h1, m1] = time1.split(':')
let [h2, m2] = time2.split(':')
return ((+h1 + (+m1 / 60)) + (+h2 + (+m2 / 60)))
}
async totaleore(id: number) {
var values= await this.repo.find({
where: { IdPreventivo: id },
});
return values;
}
..
您可以先将小时数转换为分钟数,然后您可以这样计算总小时数:
const data = [
{
"id": 1,
"nome": "prova2",
"datacreazione": "2021-09-05T08:41:29.000Z",
"costo": 23,
"ore": "08:30",
"IdPreventivo": 1,
"IdUtente": 2
},
{
"id": 2,
"nome": "prova2",
"datacreazione": "2021-09-06T08:38:26.000Z",
"costo": 23,
"ore": "08:30",
"IdPreventivo": 1,
"IdUtente": 2
}
]
function getTotalMinutes() {
return data.reduce((acc, curr) => {
const [hour, minutes ] = curr.ore.split(":");
acc += ((+hour * 60)+ (+minutes));
return acc;
},0)
}
console.log((getTotalMinutes()/60).toFixed(2));
请参阅 Array.reduce
实施。
const data = [
{
"id": 1,
"nome": "prova2",
"datacreazione": "2021-09-05T08:41:29.000Z",
"costo": 23,
"ore": "08:30",
"IdPreventivo": 1,
"IdUtente": 2
},
{
"id": 2,
"nome": "prova2",
"datacreazione": "2021-09-06T08:38:26.000Z",
"costo": 23,
"ore": "08:30",
"IdPreventivo": 1,
"IdUtente": 2
}
]
sumofhour = () => {
const sum = data.reduce((acc, curr) => {
let [h1, m1] = curr.ore.split(':');
acc += (+h1 + (+m1 / 60));
return acc;
}, 0)
return sum;
};
console.log(sumofhour());
在函数下面的 javascript 代码中,在数据库(使用 typerom)上执行一个 select,其中的数组格式如下:
JSON 数据示例:
{
"id": 1,
"nome": "prova2",
"datacreazione": "2021-09-05T08:41:29.000Z",
"costo": 23,
"ore": "08:00",
"IdPreventivo": 1,
"IdUtente": 2
},
{
"id": 2,
"nome": "prova2",
"datacreazione": "2021-09-06T08:38:26.000Z",
"costo": 23,
"ore": "08:00",
"IdPreventivo": 1,
"IdUtente": 2
}
小时数 (ore
) 字段包含 activity 的总小时数,我想做的是通过添加和执行所有 [=12] 的总和来计算总小时数=] 在该字段中为数组的每个元素输入时间,格式为 HH: MM,我该怎么做?
AttivitaprevService.js
@Injectable()
export class AttivitaprevService {
constructor(
@InjectRepository(Attivitaprev) private repo: Repository<Attivitaprev>
) {}
create(dto: CreateAttivitaprevDto) {
return this.repo.save(dto);
}
findAll() {
return `This action returns all attivitaprev`;
}
async findOne(id: number) {
return await this.repo.find({
where: { IdPreventivo: id },
});
}
sumofhour = (time1, time2) => {
let [h1, m1] = time1.split(':')
let [h2, m2] = time2.split(':')
return ((+h1 + (+m1 / 60)) + (+h2 + (+m2 / 60)))
}
async totaleore(id: number) {
var values= await this.repo.find({
where: { IdPreventivo: id },
});
return values;
}
..
您可以先将小时数转换为分钟数,然后您可以这样计算总小时数:
const data = [
{
"id": 1,
"nome": "prova2",
"datacreazione": "2021-09-05T08:41:29.000Z",
"costo": 23,
"ore": "08:30",
"IdPreventivo": 1,
"IdUtente": 2
},
{
"id": 2,
"nome": "prova2",
"datacreazione": "2021-09-06T08:38:26.000Z",
"costo": 23,
"ore": "08:30",
"IdPreventivo": 1,
"IdUtente": 2
}
]
function getTotalMinutes() {
return data.reduce((acc, curr) => {
const [hour, minutes ] = curr.ore.split(":");
acc += ((+hour * 60)+ (+minutes));
return acc;
},0)
}
console.log((getTotalMinutes()/60).toFixed(2));
请参阅 Array.reduce
实施。
const data = [
{
"id": 1,
"nome": "prova2",
"datacreazione": "2021-09-05T08:41:29.000Z",
"costo": 23,
"ore": "08:30",
"IdPreventivo": 1,
"IdUtente": 2
},
{
"id": 2,
"nome": "prova2",
"datacreazione": "2021-09-06T08:38:26.000Z",
"costo": 23,
"ore": "08:30",
"IdPreventivo": 1,
"IdUtente": 2
}
]
sumofhour = () => {
const sum = data.reduce((acc, curr) => {
let [h1, m1] = curr.ore.split(':');
acc += (+h1 + (+m1 / 60));
return acc;
}, 0)
return sum;
};
console.log(sumofhour());