Kimako
Kimako

Reputation: 625

How to make a function to recover period of times in React-native?

Iwould like to select multiple periode of times in react-native and I'm pretty lost to do it. I understand I need to use "Moment" but I'm a little stuck... For example I guess that "last week" is "moment().subtract(6, 'days').calendar();"

But how to check the last week for example from monday to last sunday ?

Thanks a lot for any help The function I"d like to set up is :

export default class MyTrips extends Component {
  constructor(props) {
    super(props);
this.state = {
      currentDate: new Date(),
      markedDate: moment(new Date()).format("YYYY-MM-DD"),
  };
  }

      execAction = (val) => {
        switch (val) {
          case "this week":
          moment().subtract(6, 'days').calendar();
            break;
          case "Last week": (from monday to last sunday)
            break;
          case "this month":
             moment().subtract(1, 'month').calendar();
            break;
          case "Last month": (last entire month)
            break;
          case "This year":
               moment().subtract(1, 'year').calendar();
            break;
          case "Last year":
            break;
          case "30 last days":
             moment().subtract(30, 'days').calendar();
            break;
          case "90 last days":
             moment().subtract(90, 'days').calendar();
              break;
          case "12 last months":
             moment().subtract(12, 'months').calendar();
            break;
      default:
          //Alert.alert("[" + val + "] No action defined...");
        }
      };

Upvotes: 0

Views: 209

Answers (1)

Petr Bela
Petr Bela

Reputation: 8741

You can call moment().startOf('week') to get to the beginning of this week, from there you can move to last week like this:

moment().startOf('week').subtract(6, 'days').calendar()

Upvotes: 1

Related Questions