+ Start a Discussion
Murali MattaMurali Matta 

Convert only Time to GMT format

Hi,

I have custom field
Type: Time
Name:Start Time    
which stores and displays the time in my local time zone(PST). How can I convert time to GMT format and display to the user?

Thanks,
Murali
Best Answer chosen by Murali Matta
Murali MattaMurali Matta
Hi Ajay/ Raj,

I got the answer. 
By using offset value we convert the time to GMT value.

Example: If a user in PST/PDT, and day light(PDT) offset value is -252000, Standard Time(PST) Offset value is -282000.
We can determine whether user is in PST/PDT and convert accordingly.


Thanks,
Murali

All Answers

Raj VakatiRaj Vakati
Use the format method ...

Refer this link 

https://developer.salesforce.com/forums/?id=9060G000000I1SjQAK

​​​​​​​
Datetime myDT = Datetime.now();
String myDate = myDT.format('h:mm a');

Datetime StartDT = Datetime.newInstanceGmt(2017,1,7,14,20,45);
system.debug('StartDT (local time): ' + StartDT.format('yyyy-MM-dd\'T\'HH:mm:ss'));
string modStartDT = StartDT.format('yyyy-MM-dd\'T\'HH:mm:ss','America/New_York'); 
system.debug('modStartDT (local time): ' + modStartDT);

 
Murali MattaMurali Matta
Hi Raj,

I have the field datatype as Time and it doesn't contain date.

Thanks,
Murali
Ajay K DubediAjay K Dubedi
Hi Murali,

You cannot directly convert Time to Gmt format.
You must take a datetime field and then convert it into Gmt format like this:

DateTime myDateTime = DateTime.newInstance(1993, 6, 6, 3, 3, 3);
String formatted = myDateTime.formatGMT('EEE, MMM d yyyy HH:mm:ss');
System.debug('date::::'+formatted);

Hope this explanation will help you.
Thanks.
Ajay Dubedi
Murali MattaMurali Matta
Hi Ajay,

But I my case I'm having only custom field Type: Time

Thanks,
Murali
Ajay K DubediAjay K Dubedi
Hi Murali,

If you want your output in Gmt format then I would suggest you take DateTime field instead of using Time field.

Hope this explanation will help you.

Thanks.
Ajay Dubedi
Murali MattaMurali Matta
Hi Ajay/ Raj,

I got the answer. 
By using offset value we convert the time to GMT value.

Example: If a user in PST/PDT, and day light(PDT) offset value is -252000, Standard Time(PST) Offset value is -282000.
We can determine whether user is in PST/PDT and convert accordingly.


Thanks,
Murali
This was selected as the best answer