DateTime TimeZone

I have a DateTime variable called MyDate that using ToString returns:
January 22, 2020 at 1:17:17 PM EST

I want to copy that date to a new variable called GMTdate and have it return:
January 22, 2020 at 6:17:17 PM GMT

What is the proper way to do this with the DateTime type?

[code]Var MyDate As DateTime = DateTime.Now(new TimeZone(“America/New_York”))
System.DebugLog(MyDate.ToString)

Var GMTdate as new DateTime(MyDate.SecondsFrom1970, new Timezone(“Etc/GMT”))
System.DebugLog(GMTdate.ToString)[/code]

Check out the Constructors for DateTime, specificially http://documentation.xojo.com/api/data_types/datetime.html.Constructor(secondsFrom1970_as_Double,timeZone_as_TimeZone%3D_Nil)

Note that if you don’t set the Locale in ToString, the actual formatting of the displayed date may be different than you expect. So be sure to check the docs on that.

Gavin, any reason why you used “Etc/GMT” and not “GMT” or even new Timezone(0) ?

I was going to post this line:

Var GMTdate As New DateTime(MyDate.SecondsFrom1970, New Timezone(0))

Edit: I see that (0) will report the Abbreviation as GMT and from the wikipedia link it say that GMT is an alias of Etc/GMT.

[quote=472361:@Alberto DePoo]Gavin, any reason why you used “Etc/GMT” and not “GMT” or even new Timezone(0) ?

I was going to post this line:

Var GMTdate As New DateTime(MyDate.SecondsFrom1970, New Timezone(0))

Edit: I see that (0) will report the Abbreviation as GMT and from the wikipedia link it say that GMT is an alias of Etc/GMT.[/quote]

GMT works just fine but most of them shouldn’t really be abbreviated. For example, there is an EST in Australia and an EST in the USA. Apple’s Foundation framework sticks with the full, unabbreviated name so I also stick with that.

https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Thanks! Based in this I created a pair of functions to convert from Local to GMT and back:

[code]Public Function LOCtoGMT(DateIn As DateTime) as DateTime
Var GMTdate as new DateTime(DateIn.SecondsFrom1970, new Timezone(“GMT”))
Return GMTdate
End Function

Public Function GMTtoLOC(DateIn As DateTime) as DateTime
Var LOCdate as new DateTime(DateIn.SecondsFrom1970, Timezone.Current)
Return LOCdate
End Function[/code]

Something to note here:

Last week we identified an issue on Windows 10 where some system defined TimeZones do not round-trip properly. That is, if you were to grab the abbreviation of TimeZone.Current and try to use it to create a new TimeZone object, you could get an exception stating “Bad TimeZone name”.

This was specifically found in the “Turks and Caicos Eastern Standard Time” TimeZone.

It should also be noted that TimeZone names are case-sensitive.