M
M
Max2015-05-24 13:12:32
Angular
Max, 2015-05-24 13:12:32

How to recolor Label?

Hello, I have this JS code:

productApp.filter('dateMark', function(){
  return function(srok, TERMIN){
    var nowDate = new Date;
    console.log(srok);
    if (Date.parse(nowDate) < ( Date.parse(srok) + ((86400000 * TERMIN)))){
      return 'Свежий товар';
    }
     else if (Date.parse(nowDate) > ( Date.parse(srok) + ((86400000 * TERMIN)))){
      return 'Термин хранения кончается';
    }
      else return 'Ошибка';
  }
});

And here is the output
<span class="label label-success" >Дата изготовления: {{product.data | dateMark : product.termin}} </span>

How to recolor this label for example in red color if this condition is met:
else if (Date.parse(nowDate) > ( Date.parse(srok) + ((86400000 * TERMIN)))){
      return 'Термин хранения кончается';

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xtalen, 2015-05-24
@maxprof

The simplest and "clumsy" option:

productApp.filter('dateMark', function(){
  return function(srok, TERMIN){
    var nowDate = new Date;
    console.log(srok);
    if (Date.parse(nowDate) < ( Date.parse(srok) + ((86400000 * TERMIN)))){
         return {text:'Свежий товар', class: 'label label-success'};
    }
     else if (Date.parse(nowDate) > ( Date.parse(srok) + ((86400000 * TERMIN)))){
         return {text:'Термин хранения кончается', class: 'label label-error'};
    }
      else return 'Ошибка';
  }
})

Here is a demo:
jsfiddle

A
Alexander Tartmin, 2015-05-25
@baskerville42

In order not to suffer as much with the simplicity of the date as you do, take a look at moment.js. An excellent library in which you can twirl with dates as you like.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question