Finding GMT time

sek

Member
Joined
Jul 2, 2009
Messages
10
Programming Experience
Beginner
Hi,

I want to get the GMT time of the system.how to find it?

I tried in 2 ways.here below i am writing the the way through which i tried

TimeZone.CurrentTimeZone.GetUtcOffset(Date.Now).ToString()
DateTime.Now.ToString("zzz")

I tested the above functions by changing the TimeZones of my system clock.

in these both ways i got the GMT time.but here i am facing once problem.its giving correct GMT time for some Time zones.but not in many of the time zones.here below i am mentioning some of those zones fo which GMT time not came correctly.

(GMT-09:00) Alaska
(GMT-08:00) Pacific Time
(GMT-07:00) Chihuahua, La Paz, Mazatlan-New
(GMT-07:00) Chihuahua, La Paz, Mazatlan-Old
(GMT-07:00) Mountain Time (US & CANADA)
(GMT-06:00) cENTRAL tIME (US & canada)
(GMT-06:00) Guadalajara,Mexico City,Monterrey-New
(GMT-06:00) Guadalajara,Mexico City,Monterrey-old


ist any way to find the GMT time exactly??
 
Hi,
what I understood with UTCNow is,

UTCNow=Now-GMT

Am i correct?

thanks for your reply.
They're not quite the same thing. Since version 2.0 of the .NET Framework the DateTime structure has had a Kind property, which indicates whether the value of the DateTime represents a local time or a UTC time. Date.UtcNow is actually equal to Date.Now.ToUniversalTime(). That's because Date.Now returns a DateTime whose Kind is Local, while Date.UtcNow returns a DateTime whose Kind is Utc. If you were to simply subtract the local GMT offset from Date.Now you'd get a Date whose value was the current UTC time but whose Kind was local. Here's some code to demonstrate:
VB.NET:
Dim localDirect As Date = Date.Now
Dim utcDirect As Date = Date.UtcNow
Dim localFromUtc As Date = utcDirect.ToLocalTime()
Dim utcFromLocal As Date = localDirect.ToUniversalTime()
Dim localMinusOffset As Date = localDirect.Subtract(TimeZone.CurrentTimeZone.GetUtcOffset(localDirect))

MessageBox.Show(localDirect.ToString("o"), localDirect.Kind.ToString())
MessageBox.Show(utcDirect.ToString("o"), utcDirect.Kind.ToString())
MessageBox.Show(localFromUtc.ToString("o"), localFromUtc.Kind.ToString())
MessageBox.Show(utcFromLocal.ToString("o"), utcFromLocal.Kind.ToString())
MessageBox.Show(localMinusOffset.ToString("o"), localMinusOffset.Kind.ToString())
As you can see, the first four values give the correct date/time and GMT offset for their Kind. The last one gives the time for GMT, while it still shows the local offset because it's Kind is Local.
 
Back
Top