0

I want to display the users previous log in time. I created a query for it and this gives me the right field. But this always shows UTC time and not the user locale. I do need to show it in the users locale timezone. How do I show it in the users locale?

@AuraEnabled
 public static LoginHistory getLoginHistory() {

    List<LoginHistory> secondLogin = [ SELECT   LoginTime 
    FROM    LoginHistory 
    WHERE   userId = :UserInfo.getUserId() 
    ORDER BY LoginTime DESC 
    LIMIT   1
    OFFSET  1];
    return ( secondLogin == null || secondLogin.isEmpty() )
    ? null
    : secondLogin.get( 0 );
}
2

I see that it is an AuraEnabled method so I assume that you are using a Lightning Component to display the information?

If so the following will display to the user the Date Time in their timezone

<ui:outputDateTime value="{![valueProvider | iteration variable].LoginTime}" 
     timeZone="{!$Locale.timezone}" 
     format="{!$Locale.datetimeFormat}"
/>
0

I believe you will have to use UserInfo class's getTimeZone() method:

getTimeZone()

Returns the current user’s local time zone.

and then convert according to the timezone, you can leverage the TimeZone Class for the offset and converting accordingly. not sure if there is another approach though (a better one =P).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.