Link to home
Start Free TrialLog in
Avatar of RobertFromSecretWeapons
RobertFromSecretWeapons

asked on

Convert GMT DateTime to EST DateTime (and vice versa)

Hi,

VB.Net Framework 4.0 question...

1 - I need to convert a string value such as this "2005-09-01T04:00:00.000000Z" which is GMT(Zulu) time to a DateTime object that represents the corresponding Date and Time in Eastern Standard Time (irrespective of the PC's time zone which this subroutine would run on).  The resultant EST Date and Time value must be adjusted for Daylight savings time if in effect or just EST if not (you get my meaning).

Additionally.
2 - Similar and somewhat conversely to the above, I also need to convert a string value such as this "2005/01/19 16:30" which would represent an EST Date and Time value to a GMT string value such as "2005-01-19T21:30:00.000000Z"

I read the MSDN article on this but my development PC is on MST and fouls up the results.
ASKER CERTIFIED SOLUTION
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In other words:
Imports System.Globalization

Module Module1
	ReadOnly zuluString = "2005-09-01T04:00:00.000000Z"
	ReadOnly localString = "2005/01/19 16:30"

	Sub Main()
		Dim utc As DateTime
		Dim local As DateTime

		Dim provider As CultureInfo = CultureInfo.CurrentCulture
		utc = DateTime.Parse(zuluString, provider, DateTimeStyles.AssumeUniversal).ToUniversalTime()
		local = DateTime.Parse(localString, provider, DateTimeStyles.AssumeLocal).ToLocalTime()

		Console.WriteLine("First let's retrieve our local time from the GMT (Zulu) string")
		utc = DateTime.SpecifyKind(utc, DateTimeKind.Unspecified)
		Console.WriteLine("GMT: {0:s}", utc)
		Console.WriteLine("EST: {0:s}", TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utc, "Eastern Standard Time"))
		Console.WriteLine("Local: {0:s}", utc.ToLocalTime())

		Console.WriteLine()
		Console.WriteLine("Now let's retrieve our GMT (Zulu) from our local time string")
		Console.WriteLine("Local: {0:s}", local)
		Console.WriteLine("EST: {0:s}", TimeZoneInfo.ConvertTimeBySystemTimeZoneId(local, "Eastern Standard Time"))
		Console.WriteLine("GMT: {0:s}", local.ToUniversalTime())
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -User generated image-saige-