Moment - Angular 生成时间减去 15min 的数组

Moment - Angular generate array of time minus 15min

我有天气卫星图像的开始日期 2019_10_08_08_00.jpg

我正在尝试生成以下数组时隙,每个时隙之间相差 15 分钟。用于循环图像,如下所示:

[
"2019_10_08_08_00",
"2019_10_08_07_45",
"2019_10_08_07_30",
"2019_10_08_07_15",
"2019_10_08_07_00",
]

代码:

let startTime = moment('YYYY_M_DD_HH_MM');
let endTime = moment('YYYY_M_DD_HH_MM');

if( endTime.isBefore(startTime) ){
  endTime.add(1, 'day');
}

let timeStops = [];

while(startTime <= endTime){
  timeStops.push(moment(startTime).format('YYYY_M_DD_HH_MM'));
  startTime.subtract(15, 'minutes');
}

console.log(timeStops);

问题是我在控制台日志中得到空数组。

错误代码:

moment.js:293 Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments: 
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: YYYY_M_DD_HH_MM, _f: undefined, _strict: undefined, _locale: [object Object]
Error

您使用了错误的 format 字符串。 'MM' 用于月份,'mm' 用于分钟。

这里还有一个工作示例

Edit: Limit loop to 10

    const now = getRoundDate();

    const timeStops = [];

    for (let i = 0; i < 10; i++) {
      now.subtract(15, 'minutes');
      timeStops.push(now.format('YYYY_M_DD_HH_mm'));
    }

    console.log(timeStops);


    getRoundDate(): moment.Moment {
        const minutes = +moment().format('m');

        if (minutes >= 0 && minutes <= 8) return date.minutes(0);
        else if (minutes >= 9 && minutes <= 18) return date.minutes(15);
        else if (minutes >= 19 && minutes <= 38) return date.minutes(30);
        else if (minutes >= 39 && minutes <= 48) return date.minutes(45);
        else if (minutes >= 49) return date.minutes(0).add(1, 'hours')
    }