Vue Moment add 6 months and use from now

Viewed 334

Is it possible to add 6 months to a date and use from now in a single line of code with Vue Moment

I was thinking something like this:

date | moment('add', '6 months', 'DD/MM/YYYY') | moment('from', 'now')

Or this

date | moment('from', 'now', 'add', '6 months') 

But these methods didn't work, any ideas? Is it even possible?

2 Answers

It's as simple as just piping one filter after other one, like in your first try, but without changing the returned format, so the Date object remains available to the next filter:

{{ date | moment("add", "6 months") | moment("from", "now") }}

See it working here:

Vue.use(vueMoment)

new Vue({
  el: "#app",
  data () {
    return {
      date: new Date(2014, 1, 1)
    }
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.29.1/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-moment@4.1.0/dist/vue-moment.min.js"></script>

<div id="app">
  <span>{{ date | moment("add", "6 months") | moment("from", "now") }}</span>
</div>

You could use chaining

date | moment("add", "6 months", "from", "now")

Edit keen-babbage-tkcp9

Related