How to get Date and Time from the Server? VB.Net Tutorial

Connecting to the server is the first factor you need to consider before getting the date and time of the server. If you are connected to, it is very easy to get what you had mentioned. As a VB.Net programmer all here is possible if there’s a whole in it, you know what I mean, right?

We just begin here, considering the fact that you already connected to the server, just prepared your project for the test mode.


Advice: If you not sure about this and you are first timer on these codes you need to test first before anything else or before inserting the code to your project. The purpose of trial and error method is to experiment the new code. It because there are some source code over the internet that not exactly correct or appropriate to the project that we are working it.

So here we go, in order to perform this task, you need to construct a function and structure. It is a very simple task. Why? Because you are just copy the code from here and paste it to your test project.

Here are the codes:
‘Paste this code above your form class
Imports System.Runtime.InteropServices

‘Paste this code before the form  end class
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

    Structure TIME_OF_DAY_INFO
        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

    Function GetNetRemoteTOD(ByVal strServerName As String) As Date
        Try
            Dim iRet As Integer
            Dim ptodi As IntPtr
            Dim todi As TIME_OF_DAY_INFO
            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)),  _
                  TIME_OF_DAY_INFO)
                NetApiBufferFree(ptodi)
                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
            Else
                ' MsgBox("Error retrieving time")
            End If
        Catch
            ' MsgBox("Error in GetNetRemoteTOD: " & Err.Description)
        End Try
        Return GetNetRemoteTOD
    End Function


Public Sub updatedate()
        On Error GoTo err1
        Dim dRemoteDate As Date
        dRemoteDate = GetNetRemoteTOD("127.0.0.1")
       MsgBox("The remote date is " & dRemoteDate)
         Exit Sub
err1:
        MsgBox("Error occurred while connecting to the Server, Please check the Server and try again!", vbCritical, "Server Connection Failed")
        End
    End Sub

Don’t worry about it, the system will automatically read the code.  The thing that you need to do is to change the IP address of your server, you can use the computer name to call it but I recommended you to use the IP address to reach the exact server you are connect to connect with.  Just enjoy and have a happy programming day!

Post a Comment

Your comment/s are highly appreciated..! Thanks for dropping here..!