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
RagingForceRagingForce 

how to convert a datetime to a specific timezone in apex?

how i am wondering if this can be done in apex say for example i am in BST (GMT+00:00) and want to convert a datetime to CET (GMT+01:00)

 

ok so if i am in london and it is 1pm and now i want to convert this to berlins time it should be 2pm

 

i am trying

 

DateTime ii = DateTime.now();

ii.format('h:mm a', 'GMT+01:00');

 

but this always returns 1pm?

ministe2003ministe2003

I think it is working and you're confused.

 

If it's 1pm in London and 2pm in Germany, you must rememer that you are in BST now and not GMT.  BST is GMT+1.

 

So what you're doing is taking the current time (which is in BST)  formatting it into GMT (taking an hour off it) and adding an hour (effectively taking you back to BST).

 

The time in Germany now is CET, which is currently +2 hours on GMT because the uk is in BST now.

RagingForceRagingForce

lol yep i am confused about all this timezone stuff :smileysurprised:

RagingForceRagingForce

regarding this statement:

 

The time in Germany now is CET, which is currently +2 hours on GMT because the uk is in BST now.

 

should i just add an extra 1 hour or how is this calculated?