Forum Discussion

kzaiger's avatar
kzaiger
New Contributor
8 years ago

Expression Function equivalent to MONTHS_BETWEEN

Is there a SnapLogic expression function equivalent to the SQL function MONTHS_BETWEEN?

I considered using getTime then dividing the difference by 2.628e+9, but it’s not exactly the same.

1 Reply

  • Rather complex expression but its needed. There may be other solutions. you can make this a function part of javascript expression library -

    $today.getFullYear() == $past.getFullYear() ? ($today.getMonth()-$past.getMonth()) : (11 - $past.getMonth()) + ($today.getMonth() + 1) + ($today.getFullYear() - $past.getFullYear() - 1) * 12

    where

    $today = Date.now()
    $past = Date.now().minusMonths(13)
    

    You can plugin any dates and it will work.

    Another expression

    (($today.getFullYear() - $past.getFullYear()) * 12) - ($past.getMonth()) + $today.getMonth()

    Yet Another expression

    ($today.getUTCMonth() - $past.getUTCMonth() ) + ( $today.getUTCFullYear() - $past.getUTCFullYear() != 0 ? ($today.getUTCFullYear() - $past.getUTCFullYear())*12 :0 )