Click here to Skip to main content
15,400,433 members
Articles / Programming Languages / Visual Basic
Posted 6 Jan 2004


24 bookmarked

Retrieving date and time from remote server using NetRemoteTOD in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.27/5 (16 votes)
6 Jan 2004
This article will show how to retrieve the date and time from a remote server using the NetRemoteTOD function


This project shows how to use the NetRemoteTOD to retrieve the date/time from a remote server using VB.NET


Add the following imports line to your code:

Imports System.Runtime.InteropServices

API Functions

Add the following functions to your code:

Private Declare Unicode Function NetRemoteTOD Lib "netapi32" ( _
  <MarshalAs(UnmanagedType.LPWStr)> ByVal ServerName As String, _
  ByRef BufferPtr As IntPtr) As Integer
Private Declare Function NetApiBufferFree Lib _
  "netapi32" (ByVal Buffer As IntPtr) As Integer


Add the following structure to your code:

Dim tod_elapsedt As Integer
Dim tod_msecs As Integer
Dim tod_hours As Integer
Dim tod_mins As Integer
Dim tod_secs As Integer
Dim tod_hunds As Integer
Dim tod_timezone As Integer
Dim tod_tinterval As Integer
Dim tod_day As Integer
Dim tod_month As Integer
Dim tod_year As Integer
Dim tod_weekday As Integer
End Structure


Add the following function to your code:

Function GetNetRemoteTOD(ByVal strServerName As String) As Date
Dim iRet As Integer
Dim ptodi As IntPtr
Dim dDate As Date
strServerName = strServerName & vbNullChar
iRet = NetRemoteTOD(strServerName, ptodi)
If iRet = 0 Then
todi = CType(Marshal.PtrToStructure(ptodi, GetType(TIME_OF_DAY_INFO)), _
dDate = DateSerial(todi.tod_year, todi.tod_month, todi.tod_day) + " " + _
TimeSerial(todi.tod_hours, todi.tod_mins - todi.tod_timezone, todi.tod_secs)
GetNetRemoteTOD = dDate
MsgBox("Error retrieving time")
End If
MsgBox("Error in GetNetRemoteTOD: " & Err.Description)
End Try
End Function

Calling the function

Here is a sample code used to call the function. "servername" is replaced with the name of the actual server that you want to get the date/time from:

Dim dRemoteDate As Date
dRemoteDate = GetNetRemoteTOD("servername")
MsgBox("The remote date is " & dRemoteDate)


That's all folks! I wanted to make this as easy to understand as possible. I am also attaching a complete sample project which has this interface:

Sample screenshot

Let me know if you have any questions.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
No Biography provided

Comments and Discussions

QuestionDoesn't work in Windows 10 Pin
Gerry Lindsay29-Dec-20 7:57
MemberGerry Lindsay29-Dec-20 7:57 
Questionnote for test Pin
Member 100456472-Jul-17 3:49
MemberMember 100456472-Jul-17 3:49 
GeneralMy vote of 1 Pin
Member 857381017-Jan-12 0:13
MemberMember 857381017-Jan-12 0:13 
GeneralDay Increment/Decrement Problem Pin
M. Ousama Ghazali7-Jul-09 20:22
MemberM. Ousama Ghazali7-Jul-09 20:22 
GeneralNetRemoteTOD Pin
cocacolacool9-Jun-09 20:41
Membercocacolacool9-Jun-09 20:41 
Generallinux version Pin
ctarsoaga5-Mar-07 7:47
Memberctarsoaga5-Mar-07 7:47 
GeneralWhere do I fill in my username and password Pin
karthik_885-Apr-06 6:16
Memberkarthik_885-Apr-06 6:16 
GeneralDoesn't work for me urgent pls... Pin
pugak23-Aug-05 18:14
Memberpugak23-Aug-05 18:14 
GeneralDoesn't work for me urgent pls... Pin
pugak23-Aug-05 18:12
Memberpugak23-Aug-05 18:12 
GeneralExtend to include milliseconds Pin
Member 17661643-Aug-05 7:44
MemberMember 17661643-Aug-05 7:44 
QuestionMust have a session first, but how in .NET ? Pin
msouphy15-Jun-05 23:10
Membermsouphy15-Jun-05 23:10 
GeneralDoesn't work with a unix server Pin
weezowazo19-Jan-05 6:19
Memberweezowazo19-Jan-05 6:19 
GeneralSame function fixed and compressed Pin
Ted14059-Nov-04 16:39
MemberTed14059-Nov-04 16:39 
QuestionRe: Same function fixed and compressed Pin
DorMarchan14-May-08 5:52
MemberDorMarchan14-May-08 5:52 
GeneralDoesn't always work Pin
Anonymous27-Sep-04 11:54
MemberAnonymous27-Sep-04 11:54 
GeneralRe: Doesn't always work Pin
Anonymous28-Sep-04 5:17
MemberAnonymous28-Sep-04 5:17 
GeneralRe: Doesn't always work - here's a cleaned up version Pin
plq110-May-08 19:00
Memberplq110-May-08 19:00 
GeneralVery Good Article... Pin
jkutsor13-Jan-04 1:18
Memberjkutsor13-Jan-04 1:18 
GeneralRe: Very Good Article... Pin
SeaWater13-Jan-04 2:14
MemberSeaWater13-Jan-04 2:14 
QuestionHow about net time Pin
hmlinder9-Jan-04 0:26
Memberhmlinder9-Jan-04 0:26 
AnswerRe: How about net time Pin
SeaWater9-Jan-04 2:38
MemberSeaWater9-Jan-04 2:38 
GeneralWhy not just use a web service Pin
hmlinder7-Jan-04 15:27
Memberhmlinder7-Jan-04 15:27 
GeneralRe: Why not just use a web service Pin
SeaWater8-Jan-04 2:52
MemberSeaWater8-Jan-04 2:52 
Smile | :) Hello, thank you for taking the time to comment on this topic. At first using a web service might seem like the logical thing to do but, after a careful consideration, there are a couple of advantages of using this api rather than a web service:

1. If you use a web service, you would need to create two separate applications. One would be the actual web service on the server which you are retrieving the date/time from using a function that returns the date/time as you described. The second application would be the consumer on the client retrieving the time. By using the NetRemoteTOD api, you only need one piece of code on the client and nothing on the server!

2. The second problem and the definite difference between using a web service and the NetRemoteTOD is that you would need to install the web service on every machine that you want to retrieve the time from! If you have 2200 servers that you want to check the date/time on, then you would need to install the web service on all 2200 servers! On the other hand, NetRemoteTOD is a time synchronization protocol that is already built into the operating system(Except For Windows 95/98), therefore you can retrieve the time from a server without installing any code on that server.

I do see a use for a web service but it would be in a different scenario, you can use the web service to host the call to NetRemoteTOD. You would pass the server name that you wish to retrieve the date/time from and then you would get back the date/time value for that server. Then you would just have one server which retrieves the time from all the machines using just one web service.

Thanks and I hope this clears up the use of NetRemoteTOD,
GeneralRe: Why not just use a web service Pin
Anonymous4-Feb-04 6:40
MemberAnonymous4-Feb-04 6:40 
GeneralRe: Why not just use a web service Pin
Anonymous4-Feb-04 7:03
MemberAnonymous4-Feb-04 7:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.