Search code examples
vue.jsstorevuex

clone and copy vuex store state moment date


Having a state like:

this.$store.state.fromDate

I would like to define a variable like this:

 var newFrom = this.$store.state.fromDate.subtract(1, 'days');

Without modifying the value of this.$store.state.fromDate

Is it possible?


Solution

  • You can clone the moment date:

    // First, clone the moment date object
    var newFrom = this.$store.state.fromDate.clone();
    // Then, subtract
    newFrom.subtract(1, 'days');
    

    This will not subtract to the this.$store.state.fromDate but to the newFrom

    Or, in a single line:

    var newFrom = this.$store.state.fromDate.clone().subtract(1, 'days');