Solved

# Convert time to UTC

Posted on 2008-11-01
1,140 Views
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
Question by:DColin
• 3
• 2
• 2

LVL 31

Expert Comment

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

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

ID: 22860970
Juan_Barrera:

Yes you have identified the problem correctly.
0

LVL 31

Expert Comment

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

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

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

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)

``````
0

## Featured Post

### Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…