Question

Photo of Arran France

0

Using Liquid to Calculate Dates Since Last Logon

Hey there,

I'm trying to use Liquid to calculate the number of days since a user signed up to offer them helpful tips in their first 

Using the merge {% for User in Person.Users %} {{ User.CreatedDateTime }} {% endfor %} produces two dates and times, whereas using { User.CreatedDateTime }} produces one.
I'm assuming this is what's called an array but the Lava documentation isn't too clear on how to use these.

Could anyone give me an example of how to use these alongside filters and if statements

  • Photo of Rock RMS

    0

    You are correct that the User property is an array (or collection) of user logins. There's a couple of ways to treat collections in Lava. The code below is probably the best solution for your need since you probably want the oldest login creation date.

    {% assign daysSinceFirstAccount = Person.Users[0].CreatedDateTime | DateDiff:'Now','d' %}
    
    Days Since First Account: {{ daysSinceFirstAccount }} <br>
    
    {% if daysSinceFirstAccount > 7 and daysSinceFirstAccount < 30 %}
        You are pretty new here...
    {% elseif daysSinceFirstAccount < 90 %}
        You've been here before haven't you...
    {% else %}
        You're an old-timer...
    {% endif %}
    
    
    • Arran France

      This is a reply to a comment from a while back! Could you explain the use of Person.Users[0], I can't find any documentation explaining why the [0] was included. Is this what fetches the oldest date an account was created?
      If so, what would the behaviour be if I included a 1 or a 2 instead?

    • Rock RMS

      Person.Users is an array of user accounts for the person. The [0] notation grabs the first one (arrays using zero based indexes so 0 = 1). Since the person is logged in you can assume there is at least one login. If you used [1] you would get the second. Most people though will only have one so you'd most likely get a null back (or possibly an exception :)

    • Arran France

      That's cleared that up thanks. I'm assuming Person.Users[0].LastLoginDateTime will record their active session as their last login so the value will always be "now" if they're logged in. Is there any way of fetching the login prior to that using Lava?