function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RustyboyRustyboy 

Format time in users locale

Hi,

I have a rquirement to format a time which is given in HH:MM format (24-hour clock) into the user's locale. I assumed that there would be a fromat() method as the DateTime class does, but it does not exist.

Any ideas how I can do this?

Thanks
Best Answer chosen by Rustyboy
Mahesh DMahesh D
Hi Rusty,

Please find the below code:
 
public class Utility {
    public static Datetime getLocalDateTime(Datetime z)
    {    
        Datetime l = z.Date();
        l = l.addHours(z.hour());
        l = l.addMinutes(z.minute());
        l = l.addSeconds(z.second());
        
        return l;
    }
}

----------------------

Datetime gmt = Datetime.Now();

Datetime local = Utility.getLocalDateTime(Datetime.Now());

System.Debug('GMT Time: ' + gmtNow);

System.Debug('Local Time: ' + local);

Another example:
 
Datetime current = System.now(); // returns date time value in GMT time zone.

Date currDate = current.date();

Date currTime = current.time();

Datetime local = datetime.newinstance(currDate,currTime); // This will return date time in my local time zone.

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Rusty,

Please find the below code:
 
public class Utility {
    public static Datetime getLocalDateTime(Datetime z)
    {    
        Datetime l = z.Date();
        l = l.addHours(z.hour());
        l = l.addMinutes(z.minute());
        l = l.addSeconds(z.second());
        
        return l;
    }
}

----------------------

Datetime gmt = Datetime.Now();

Datetime local = Utility.getLocalDateTime(Datetime.Now());

System.Debug('GMT Time: ' + gmtNow);

System.Debug('Local Time: ' + local);

Another example:
 
Datetime current = System.now(); // returns date time value in GMT time zone.

Date currDate = current.date();

Date currTime = current.time();

Datetime local = datetime.newinstance(currDate,currTime); // This will return date time in my local time zone.

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Option 1:-
If you are looking for Formula field please check below post
1) https://help.salesforce.com/apex/HTViewHelpDoc?id=formula_examples_dates.htm&language=en_US#getting-hms-from-datetime
IF(
  OR(
    VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) = 0,
    VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) = 12
  ),
  "12",
  TEXT( VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) 
   -
   IF( 
     VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) < 12, 
     0, 
     12 
   ) 
  )
)
& ":" &
MID( TEXT( date/time - TZoffset ), 15, 2 )
& ":" &
MID( TEXT( date/time - TZoffset ), 18, 2 )
& " " &
IF(
  VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) < 12,
  "AM",
  "PM"
)
1) https://help.salesforce.com/HTViewSolution?id=000181642
How do I convert a Date/Time field for any timezone?
IF ( DATEVALUE(QA_ETOC__c) >=
DATE ( YEAR (DATEVALUE(QA_ETOC__c)),3,1)
+
(
14-
CASE( MOD(DATE ( YEAR (DATEVALUE(QA_ETOC__c)),3,1) - DATE (1900,1,7),7) ,
0,7,MOD(DATE ( YEAR (DATEVALUE(QA_ETOC__c)),3,1) - DATE (1900,1,7),7))
)
&&
DATEVALUE(QA_ETOC__c) <
DATE ( YEAR (DATEVALUE(QA_ETOC__c)),11,1)
+
(
7-
CASE( MOD(DATE ( YEAR (DATEVALUE(QA_ETOC__c)),11,1) - DATE (1900,1,7),7) ,
0,7,MOD(DATE ( YEAR (DATEVALUE(QA_ETOC__c)),11,1) - DATE (1900,1,7),7))
),
LEFT ( TEXT (QA_ETOC__c- 7/24 ), 16),
LEFT ( TEXT (QA_ETOC__c- 8/24), 16)
)


Option 2:-
Please check below post for some example
1) https://developer.salesforce.com/forums/?id=906F00000008wZxIAI
2) http://salesforce.stackexchange.com/questions/8538/convert-time-from-a-different-timezone-other-than-local-to-gmt

Option 3:- How to get Current User Time zone

Exmple 1:-
TimeZone tz = UserInfo.getTimeZone();
System.debug('Display name: ' + tz.getDisplayName());
System.debug('ID: ' + tz.getID());
// During daylight saving time for the America/Los_Angeles time zone
System.debug('Offset: ' + tz.getOffset(DateTime.newInstance(2012,10,23,12,0,0)));
// Not during daylight saving time for the America/Los_Angeles time zone
System.debug('Offset: ' + tz.getOffset(DateTime.newInstance(2012,11,23,12,0,0)));
System.debug('String format: ' + tz.toString());
Exmple 2:-
DateTime currenttime = DateTime.now();
System.debug('--------------->'+currenttime.format('h:mm a', 'GMT+05:30'));




NOTE:-  Date and Date/Time values are stored in GMT. When a record is saved, field values are adjusted from the user’s time zone to GMT, and then adjusted back to the viewer’s time zone when displayed in record detail pages and reports. With Date conversions this doesn't pose a problem, since converting a Date/Time to a Date results in the same Date value.

Let us know if this will help

Thanks
Amit Chaudhary
RustyboyRustyboy
Thanks for the prompt answers. In the end I used the answer suggested by Mahesh and it works well