Forum Discussion

jonathancrow's avatar
14 years ago

Comparing Last Login Date to Current Date

I am having trouble comparing dates. Can someone help?

 

Currently I am trying to use Freemarker to figure out the current date. But

 

<#assign my_now = datesupport.now />
${my_now}

 

returns

 

lithium.eval.velocity.DateDisplayTemplateModel@b566ec

 

Not exactly what I was looking for. Can you tell me how to find the current date?

 

Also, once I get the date, do I need to refromat it? Or can I simply do a 

 

<#if user.lastVisitTime < current time />

 

...

 

Thanks,

Jonathan

  • Hi Jonathan,

     

    When using the datesupport custom context object, you'll need to use one of the string methods (ie. getDateAsString()) prior to trying to output the date.

     

    If you want to compare the dates, you may be able to convert into a format that freemarker would recognize as a date, but I've found it easier to just convert the dates to milliseconds and work with them that way. Here are some examples I just brewed up:

     

    <#assign date_now_number = datesupport.now.millisecondsAsString?number />
    date_now_number: ${date_now_number}<br />
    <#assign date_yesterday_number = datesupport.now.addDay(-1).millisecondsAsString?number />
    date_yesterday_number: ${date_yesterday_number}<br />
    <#assign date_tomorrow_number = datesupport.now.addDay(1).millisecondsAsString?number />
    date_tomorrow_number: ${date_tomorrow_number}<br />

     When it comes to comparing, keep in mind that a more recent date/time will have a larger value, so:

    <#if date_now_number gt date_yesterday_number>
    Now comes after yesterday<br />
    <#else>
    Yesterday comes after now<br />
    </#if>
    
    <#if date_now_number lt date_tomorrow_number>
    Tomorrow comes after now<br />
    <#else>
    Now comes after tomorrow<br />
    </#if>

    So in those two examples above, the else condition should never be satisfied. These examples are kind of silly though, and not very useful. I just wanted to demonstrate how the comparisons worked.

     

    Let's get a bit closer to what you're trying to do:

    <#assign date_visit = rest("/users/id/1/last_visit_time").value?datetime("yyyy-MM-dd") />
    <#assign date_visit_number = datesupport.setDate(date_visit).millisecondsAsString?number />
    date_visit_number: ${date_visit_number}<br />
    
    <#assign date_weekago_number = datesupport.now.addDay(-7).millisecondsAsString?number />
    date_weekago_number: ${date_weekago_number}<br />
    
    <#if date_visit_number gt date_weekago_number>
    Visited within the last week!<br />
    <#else>
    Has not visisted within the last week.<br />
    </#if>

     

    This example demonstrates how to get the date from the REST API and convert it into something useable for comparison. Note that if you want to get a more granular value for date_visit, you may need to play with the date formatting a bit. Right now it's just grabbing the day, month, and year.

     

    I hope this helps!

2 Replies

  • AdamN's avatar
    AdamN
    Khoros Oracle
    14 years ago

    Hi Jonathan,

     

    When using the datesupport custom context object, you'll need to use one of the string methods (ie. getDateAsString()) prior to trying to output the date.

     

    If you want to compare the dates, you may be able to convert into a format that freemarker would recognize as a date, but I've found it easier to just convert the dates to milliseconds and work with them that way. Here are some examples I just brewed up:

     

    <#assign date_now_number = datesupport.now.millisecondsAsString?number />
    date_now_number: ${date_now_number}<br />
    <#assign date_yesterday_number = datesupport.now.addDay(-1).millisecondsAsString?number />
    date_yesterday_number: ${date_yesterday_number}<br />
    <#assign date_tomorrow_number = datesupport.now.addDay(1).millisecondsAsString?number />
    date_tomorrow_number: ${date_tomorrow_number}<br />

     When it comes to comparing, keep in mind that a more recent date/time will have a larger value, so:

    <#if date_now_number gt date_yesterday_number>
    Now comes after yesterday<br />
    <#else>
    Yesterday comes after now<br />
    </#if>
    
    <#if date_now_number lt date_tomorrow_number>
    Tomorrow comes after now<br />
    <#else>
    Now comes after tomorrow<br />
    </#if>

    So in those two examples above, the else condition should never be satisfied. These examples are kind of silly though, and not very useful. I just wanted to demonstrate how the comparisons worked.

     

    Let's get a bit closer to what you're trying to do:

    <#assign date_visit = rest("/users/id/1/last_visit_time").value?datetime("yyyy-MM-dd") />
    <#assign date_visit_number = datesupport.setDate(date_visit).millisecondsAsString?number />
    date_visit_number: ${date_visit_number}<br />
    
    <#assign date_weekago_number = datesupport.now.addDay(-7).millisecondsAsString?number />
    date_weekago_number: ${date_weekago_number}<br />
    
    <#if date_visit_number gt date_weekago_number>
    Visited within the last week!<br />
    <#else>
    Has not visisted within the last week.<br />
    </#if>

     

    This example demonstrates how to get the date from the REST API and convert it into something useable for comparison. Note that if you want to get a more granular value for date_visit, you may need to play with the date formatting a bit. Right now it's just grabbing the day, month, and year.

     

    I hope this helps!

  • jonathancrow's avatar
    jonathancrow
    Advisor
    14 years ago

    AdamN,

     

    Worked great. I had to modify to check out the user.id number

     

    <#assign my_userid = user.id />

    <#assign date_visit = rest("/users/id/" + my_userid + "/last_visit_time").value?datetime("yyyy-MM-dd") />
    <#assign date_visit_number = datesupport.setDate(date_visit).millisecondsAsString?number />
    date_visit_number: ${date_visit_number}<br />

     

    But got what I needed. 

     

    Thanks,

    Jonathan