Skip to content
Groovy -23 – Return Current Date and Time Based on the Timezone Of The User

Sales Cloud provides Groovy methods to return the date and time in UTC/GMT, regardless of the user's timezone set in User Preferences. To address this, we need to build a custom function that adjusts to the user's timezone.

The following Groovy script retrieves the user's timezone, adjusts the current time accordingly using the Calendar and TimeZone classes, and formats the date and time:

        
        def var_securityContext = adf.context.getSecurityContext()
        def var_UserTimeZone = var_securityContext.getUserProfile().getTimeZone()
        
        Calendar var_calendar = Calendar.getInstance()
        TimeZone var_tz = TimeZone.getTimeZone(var_UserTimeZone)
        
        var_calendar.add(Calendar.MILLISECOND, -(var_calendar.getTimeZone().getRawOffset()))
        var_calendar.add(Calendar.MILLISECOND, var_tz.getRawOffset())
        
        Date myDate = new Date(var_calendar.getTimeInMillis())
        def var_format = new Formatter()
        
        def String result = var_format.format('%tm/%td/%tY %tH:%tM:%tS', myDate, myDate, myDate, myDate, myDate, myDate)
        return result
        
        

The above code returns the current date and time in the user's timezone, formatted as: MM/DD/YYYY HH:MM:SS

To display the time in a 12-hour format with AM/PM, use the following code:

        
        def var_securityContext = adf.context.getSecurityContext()
        def var_UserTimeZone = var_securityContext.getUserProfile().getTimeZone()
        
        Calendar var_calendar = Calendar.getInstance()
        TimeZone var_tz = TimeZone.getTimeZone(var_UserTimeZone)
        
        var_calendar.add(Calendar.MILLISECOND, -(var_calendar.getTimeZone().getRawOffset()))
        var_calendar.add(Calendar.MILLISECOND, var_tz.getRawOffset())
        
        Date myDate
        /* Fix for specific time zones */
        if (var_UserTimeZone == 'America/Santiago' || 
            var_UserTimeZone == 'America/Sao_Paulo' || 
            var_UserTimeZone == 'Australia/Sydney' || 
            var_UserTimeZone == 'Australia/Brisbane' || 
            var_UserTimeZone == 'Australia/Hobart') {
            myDate = new Date(var_calendar.getTimeInMillis() + 3600000)
        } else {
            myDate = new Date(var_calendar.getTimeInMillis())
        }
        
        Calendar v_Calendar = Calendar.getInstance()
        v_Calendar.setTime(myDate)
        
        String hours = v_Calendar.get(Calendar.HOUR_OF_DAY)
        String minutes = v_Calendar.get(Calendar.MINUTE)
        String mergeTime = hours + "." + minutes
        double period = Double.valueOf(mergeTime)
        
        def var_formatD = new Formatter()
        def var_formatH = new Formatter()
        def var_formatMS = new Formatter()
        
        String v_date = var_formatD.format('%tm/%td/%tY ', myDate, myDate, myDate)
        String v_hour = var_formatH.format('%tH', myDate)
        String v_minutesSec = var_formatMS.format(':%tM:%tS', myDate)
        
        String DisplayTime = ""
        
        if (period >= 0.0 && period <= 11.59) {
            if (v_hour == "00") {
                DisplayTime = v_date + " " + "12" + v_minutesSec + " AM"
            } else {
                DisplayTime = v_date + " " + v_hour + v_minutesSec + " AM"
            }
        } else if (period >= 12.0 && period <= 23.59) {
            switch (v_hour) {
                case "12":
                    DisplayTime = v_date + " " + v_hour + v_minutesSec + " PM"
                    break
                case "13":
                    DisplayTime = v_date + " " + "01" + v_minutesSec + " PM"
                    break
                case "14":
                    DisplayTime = v_date + " " + "02" + v_minutesSec + " PM"
                    break
                case "15":
                    DisplayTime = v_date + " " + "03" + v_minutesSec + " PM"
                    break
                case "16":
                    DisplayTime = v_date + " " + "04" + v_minutesSec + " PM"
                    break
                case "17":
                    DisplayTime = v_date + " " + "05" + v_minutesSec + " PM"
                    break
                case "18":
                    DisplayTime = v_date + " " + "06" + v_minutesSec + " PM"
                    break
                case "19":
                    DisplayTime = v_date + " " + "07" + v_minutesSec + " PM"
                    break
                case "20":
                    DisplayTime = v_date + " " + "08" + v_minutesSec + " PM"
                    break
                case "21":
                    DisplayTime = v_date + " " + "09" + v_minutesSec + " PM"
                    break
                case "22":
                    DisplayTime = v_date + " " + "10" + v_minutesSec + " PM"
                    break
                case "23":
                    DisplayTime = v_date + " " + "11" + v_minutesSec + " PM"
                    break
            }
        }
        return DisplayTime
        
        

At this point, DisplayTime will show the time in the format: MM/DD/YYYY HH:MM:SS AM/PM

You can use any of the above code (for 24-hour or 12-hour format) to create a global function that returns a string. Then, use this global function reference in your triggers or formula fields.