使用ReactJs中的MomentJs获得给定2个日期的日期数组 [英] Get array of dates given 2 dates using MomentJs in ReactJs

查看:847
本文介绍了使用ReactJs中的MomentJs获得给定2个日期的日期数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在按以下方式输入Moment和Moment范围, 需要给定2个日期之间的日期数组,以便可以迭代日期

Currently i'm importing Moment and Moment range as below, Need the array of dates between given 2 dates so that dates can be iterated

import Moment from 'moment';
import {extendMoment} from 'moment-range';

import React, {Component} from 'react';

class T extends Component {
  constructor(props) {
    const start = moment("2018-10-14", 'YYYY-MM-DD');
    const end = moment("2018-10-20", 'YYYY-MM-DD');
    const range = moment.range(start, end);
    //need an array of dates in'YYYY-MM-DD' to itarate
  }
}

推荐答案

要执行您要的操作,因为您已经拥有moment-range,您将执行以下操作:

To do what you are asking since you already have moment-range you would do something like this:

const range = moment.range(moment("2018-10-14"), moment("2018-10-20"));

console.log(Array.from(range.by('day')).map(x => x.format('YYYY-MM-DD')))

如果您不需要立即进行格式化,则只需:

If you do not need to format right away it would simply be:

Array.from(range.by('day'))  // for your range

这是节点中的示例,但在您的情况下,该示例与之相似,只是导入

Here is an example in node but it would be similar in your case just import by

这篇关于使用ReactJs中的MomentJs获得给定2个日期的日期数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆