converting month to year usig js in laravel project

Viewed 58

if we consider X as month i want to check if X was bigger than 24, then divide it in 12 and return a number otherwise if it was smaller than 24 i want the code to return X itself.

note that the project is in Laravel and i want to do this with inline JavaScript code.

i tried to do it this way

<script>
const guaranteeTime = "{{$product->guarantee_time}}"

function guaranteeConvert() {
   const guaranteeTimeNew = guaranteeTime / 12;
}

if (guaranteeTime > 24) {
  guaranteeConvert();
}



</script>

thnaks

3 Answers

You can get php variable in JS like this:

const guaranteeTime = {!! json_encode($product->guarantee_time) !!};

You need to parse value to Integer. Like below

const guaranteeTimeNew = parseInt(guaranteeTime) / 12;

Hi There You can do it Like this :

function MonthToYear(x) {
let result='';
if(x>24)
       {
         result=parseInt(x/12)+"Year/s";
         if(x%12!=0)
         result=result+" and "+x%12+" Month/s";
        
         return result;}
else return x;
                         }
Related