Typescript 将momentjs moment取整为时间单位中最接近的分钟

Typescript 将momentjs moment取整为时间单位中最接近的分钟,typescript,momentjs,Typescript,Momentjs,我想改变时间,就像跟着时间一样 当时间单位为30分钟时 2019-12-05 10:36:33->2019-12-05 11:00:00 2019-12-05 10:13:33->2019-12-05 10:30:00 当时间单位为60分钟时 2019-12-05 10:01:33->2019-12-05 11:00:00 2019-12-05 10:50:33->2019-12-05 11:00:00 我如何才能简单地做到这一点???尝试以下代码: import * as moment fro

我想改变时间,就像跟着时间一样

当时间单位为30分钟时

2019-12-05 10:36:33->2019-12-05 11:00:00

2019-12-05 10:13:33->2019-12-05 10:30:00

当时间单位为60分钟时

2019-12-05 10:01:33->2019-12-05 11:00:00

2019-12-05 10:50:33->2019-12-05 11:00:00

我如何才能简单地做到这一点???

尝试以下代码:

import * as moment from 'moment';

function roundTime(input: string, uint: number) {
    let time_unit = uint;
    let minute = moment(input).minutes();
    let step = (((minute / time_unit) + 1) * time_unit) - (minute % time_unit);
    return moment(input).minute(0).second(0).add(step, 'minute').format('YYYY-MM-DD HH:mm:ss');
}

let out1 = roundTime('2019-12-05 10:36:33', 30) ;
let out2 = roundTime('2019-12-05 10:13:33', 30) ;
let out3 = roundTime('2019-12-05 10:01:33', 60) ;
let out4 = roundTime('2019-12-05 10:50:33', 60) ;
console.log(out1);
console.log(out2);
console.log(out3);
console.log(out4);