Check if published date is older than 6 months


(Steven) #1

Hi,

I’m putting together a list of articles and I want to display an “archive” label if the last published falls outwith the last 6 months.

I have some test code below but it fails when I try to add my date variable to the keyword modifier line at the bottom.

I’d really appreciate it if you could take a look.

Thanks
Steven

function addMonths(date, months) {
          date.setMonth(date.getMonth() + months);
          return date;
        }

        var archiveDate = addMonths(new Date(), -6); // six months ago
        
        function formatDate(date) {
            var d = new Date(date),
                month = '' + (d.getMonth() + 1),
                day = '' + d.getDate(),
                year = d.getFullYear();
        
            if (month.length < 2) 
                month = '0' + month;
            if (day.length < 2) 
                day = '0' + day;
        
            return [year, month, day].join('-');
        }
        
        // print(formatDate(archiveDate));  = 2022-08-13
        
        // print ('%asset_published^gte_date:2022-08-13:NEW:OLD%'); = This works if I type the date into the modifier rather than have a dynamic date.
        
        // the dynamic date below always shows OLD even if it shouldnt.
        print ('%asset_published^gte_date:' + formatDate(archiveDate) + ':NEW:OLD%');

(Steven) #2

I solved my issue through SSJS:

 <script runat="server">
            var myDate = %globals_date_U%;
            var myAsset = %asset_published_U%;
            
            var diff = myDate-myAsset;
            if(diff > 15811200){
                print('<span class="fc-archive-ribbon"><i class="fa fa-clock-o" aria-hidden="true"></i> This article is more than 1 year old</span>');
            }
        </script>