Solved

Convert time to UTC

Posted on 2008-11-01
7
1,143 Views
Last Modified: 2012-05-05
Hi Experts,

I would like to convert a time (Always Netherlands time) to UTC but the converting software will run on computers set to different time zones London, New York etc.
0
Comment
Question by:DColin
  • 3
  • 2
  • 2
7 Comments
 
LVL 31

Expert Comment

by:Frosty555
ID: 22860365
vb.net's date object lets you convert to UTC time pretty easily.

Dim a as Date = Now()          <--- or set to whatever date you want in whatever timezone
a.ToUniversalTime()              <--- returns utc date
0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22860421
The problem is that he will be trying to convert a Netherlands time using, for example, a London based system. The .net datetime object relies on the windows system time api, so it will fail.
But, if you can determine the timezone on where the code is running, then you can find the difference between that zone and the Netherlands zone, "convert" the time from NEtherlands to the new zone and, finally, call the function time.ToUniversalTime
0
 

Author Comment

by:DColin
ID: 22860970
Juan_Barrera:

Yes you have identified the problem correctly.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 31

Expert Comment

by:Frosty555
ID: 22863249
Hmm so then you can convert local time to UTC time. Then, since UTC is a standard, and netherlands is +01:00 UTC, we can just add one hour to the UTC time to get the netherlands time. Then convert that time back to UTC time and presto.

Not sure how daylight savings would play into this but I believe it will be taken into account in the conversion to UTC time.

        Dim localtime As Date = Now()
        Dim nethertime As Date = localtime.ToUniversalTime().AddHours(1)
        Dim newutctime As Date = nethertime.ToUniversalTime()

Give that a try. If that works, point split between Juan and me.
0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22863906
DColin:

Try this:

'Find Amsterdam TimeZone info
Dim tst As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time")

'Declare the Amsterdam time you want to convert
Dim time As DateTime = "YourAmsterdamTime"

'Substract UTC offset, as Amsterdam will always be ahead
time = time.Subtract(tst.BaseUtcOffset)

For more inf oabout the TimeZone class:
http://msdn.microsoft.com/en-us/library/system.timezone.aspx

0
 

Author Comment

by:DColin
ID: 22868516
Hi Juan_Barrera:

This is almost working but the only problem is that it is not taking into account Dutch daylight savings time.
0
 
LVL 20

Accepted Solution

by:
Juan_Barrera earned 500 total points
ID: 22870200
Does the following work with Daylight savings?
 

'Find Amsterdam TimeZone info

Dim tst As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time")

'Declare the Amsterdam time you want to convert

Dim time As DateTime = "AmsterdamTime"

TimeZoneInfo.ConvertTimeToUtc(time, tst)
 

More info on: http://msdn.microsoft.com/en-us/library/bb495915.aspx

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now