1

I want to give text color after two date with vue js, but I couldn't find a solution. Example: I want to make the StartDate Today and the End Date 05.10.2021 red accordingly. but I want to do this condition with v-bind is it possible?

<tr v-for="(item, i) in attributes" :class="today ? 'greater' : 'today'">
<td>{{ today ,item.due_date | remainingday}}</td>
</tr>

export default {
  data() {
    return {
      type: 'sales_invoices',
      attributes: [],
      start: new Date().toLocaleDateString,
    }
  },
  async created() {
    const res = await this.callApi('get', this.type)
    if (res.status === 200) {
      this.attributes = res.data.data
    }
  },
  computed: {
    today(){
      const startY = new Date(this.start.split(".").reverse().join("."))
      const end = new Date('05.10.2021'.split(".").reverse().join("."))
      const difDays = Math.floor((startY.getTime() - end.getTime()) / (1000 * 3600 * 24))
      return difDays > 0
    },
  },

app.js

Vue.filter('remainingday', function (startDate, endDate) {
  const start = new Date(startDate.split(".").reverse().join("."))
  const end = new Date(endDate.split(".").reverse().join("."))
  if(start.toDateString() === end.toDateString()) {
    return 'TODAY'
  } 
  const difDays = Math.floor((start.getTime() - end.getTime()) / (1000 * 3600 * 24))
  return difDays > 0 
    ? `PAYMENT DELAYED FOR ${difDays} DAYS `
    : `${difDays * -1} DAYS LEFT`
});

1 Answer 1

1

You can try with class binding and method:

new Vue({
  el: '#demo',
  data(){
    return {
      start: new Date(),
      attributes: [{due_date: '30.10.2021'}, {due_date: '07.10.2021'}, {due_date: '03.10.2021'}]
    }
  },
  methods: {
    remainingday (startDate, endDate) {
      const dates = startDate.toLocaleDateString() + ' ' + endDate
      const start = new Date(startDate)
      const end = new Date(endDate.split(".").reverse().join("-"))
      if(start.toDateString() === end.toDateString()) return {dat: `${dates} - TODAY`, cl: 'today'}
      const difDays = Math.ceil((start.getTime() - end.getTime()) / (1000 * 3600 * 24))
      return difDays > 0 
        ? {dat: `${dates} - PAYMENT DELAYED FOR ${difDays} DAYS`, cl: 'greater'}
        : {dat: `${dates} - ${difDays * -1} DAYS LEFT`, cl: ''}
    }
  }
})

Vue.config.productionTip = false
Vue.config.devtools = false
.today {
  color: blue; 
}
.greater {
  color: red; 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<table>
    <tr v-for="(item, i) in attributes" :key="i">
      <td :class="remainingday(start, item.due_date).cl">{{ remainingday(start, item.due_date).dat }}</td>
    </tr>
  </table>
</div>

Sign up to request clarification or add additional context in comments.

11 Comments

Thank you for your answer, but due to the global change of my filters, it's not possible for me to do that based on your answer. What I want is "today date and if it is greater than date in database, I just want to specify text color as style or class.
my point is, if start and end are the same, class "blue", otherwise "IF TODAY'S DATE IS GREAT, THEN CLASS RED FROM THE SPECIFIED DATE"
You could also use some helper library for formatting dates , for example Day,js
I have a question. "start:new date" that's ok. but I get an error because my end date is from database and I have listed it with "async created() {".
I edited the question. date from database "DAY.MONTH.YEAR" "06.10.2021"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.