genexus

Display Datediff as Friendly text like "yesterday"


Im wondering if we have any datetime function to format date diff as friendly text. Ex: "A few hours ago", "Yesterday", "A month ago"

Im workinkg with 18.4 .Net and SQL.

Maybe there is a format option or a conver to friendlystring.


Solution

  • maybe this isn't the best answer you'll get, but here's a code I made. I hope it helps you.

    &DateTime = Date.New(2023,8,1)
    
    if TDiff(now(), &DateTime) < 60
        &DifSecs = TDiff(now(), &DateTime)
        &PostDate = &DifSecs.ToString() + ' SECONDS AGO'
    else
        &DifSecs = TDiff(now(), &DateTime)
        if TDiff(now(), &DateTime) < 60 * 60
            &DifSecs = TDiff(now(), &DateTime) / 60
            if &DifSecs = 1
                &PostDate = &DifSecs.ToString() + ' MINUTE AGO'
            else
                &PostDate = &DifSecs.ToString() + ' MINUTES AGO'
            endif
        else
            if TDiff(now(), &DateTime) < 60 * 60 * 24
                &DifSecs = TDiff(now(), &DateTime) / (60 * 60)
                if &DifSecs = 1
                    &PostDate = &DifSecs.ToString() + ' HOUR AGO'
                else
                    &PostDate = &DifSecs.ToString() + ' HOURS AGO'
                endif
            else
                if TDiff(now(), &DateTime) < 60 * 60 * 24 * 30 
                    &DifSecs = TDiff(now(), &DateTime) / ((60 * 60) * 24)
                    if &DifSecs = 1
                        &PostDate = 'YESTERDAY'
                    else
                        &PostDate = &DifSecs.ToString() + ' DAYS AGO'
                    endif
                else
                    if TDiff(now(), &DateTime) < 60 * 60 * 24 * 30 * 12
                        &DifSecs = TDiff(now(), &DateTime) / (((60 * 60) * 24) * 30)
                        if &DifSecs = 1
                            &PostDate = &DifSecs.ToString() + ' MONTH AGO'
                        else
                            &PostDate = &DifSecs.ToString() + ' MONTHS AGO'
                        endif
                    else
                        &DifSecs = TDiff(now(), &DateTime) / ((((60 * 60) * 24) * 30) * 12)
                        if &DifSecs = 1
                            &PostDate = &DifSecs.ToString() + ' YEAR AGO'
                        else
                            &PostDate = &DifSecs.ToString() + ' YEARS AGO'
                        endif
                    endif
                endif
            endif
        endif
    endif