How to convert DateTime into different timezones?

Sergey picture Sergey · Oct 8, 2014 · Viewed 9.9k times · Source

How to convert DateTime into different timezones? The DateTime class has two methods .toLocal() and .toUtc(). But if I want to display time in another time zone. How can I do it?

Answer

Günter Zöchbauer picture Günter Zöchbauer · Oct 8, 2014

DateTime doesn't contain timezone information therefore you can't create a DateTime in a specific timezone only the timezone of your system and UTC are available.

You can wrap the DateTime in a custom class and add timezone information to the wrapper. You also need a table of offsets for each timezone and then add/substract the offset from the UTC date.