none
How to add some time to internet time + change windows 7/10 time and date with this RRS feed

  • Question

  • Hello,

    please help,

    Code Sample From: https://social.msdn.microsoft.com/Forums/vstudio/en-US/25044a67-35ed-4f7e-a48f-a7d5b031f107/getting-internet-time?forum=vbgeneral&prof=required

    Questions: 

    1) I need to add 5 hours, 29 minutes and 59 seconds to internet time

    2) Change windows 7/10 Date and Time with Internet Time

    Why:

    1) Hard to find correct time server for (UTC+05:30) Sri Jayawardenepura - Sri Lanka

    2) If PC have Date Time issue need fix it

    My Code:

    Imports System.IO
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Public Shared LastSysTime As DateTime
    
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
            Label1.Text = GetNISTTime("time.nist.gov").ToString
            Label2.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss tt")
        End Sub
    
        Public Function GetNISTTime(ByVal host As String) As DateTime
    
            Dim timeStr As String
    
            Try
                Dim reader As New StreamReader(New TcpClient(host, 13).GetStream)
                LastSysTime = DateTime.UtcNow()
                timeStr = reader.ReadToEnd()
                reader.Close()
            Catch ex As SocketException
                Label1.Text = ex.Message
            Catch ex As Exception
                Label1.Text = ex.Message
            End Try
    
            Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5))
            Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2))
            Dim mo As Integer = Integer.Parse(timeStr.Substring(10, 2))
            Dim dy As Integer = Integer.Parse(timeStr.Substring(13, 2))
            Dim hr As Integer = Integer.Parse(timeStr.Substring(16, 2))
            Dim mm As Integer = Integer.Parse(timeStr.Substring(19, 2))
            Dim sc As Integer = Integer.Parse(timeStr.Substring(22, 2))
            Dim Temp As Integer = CInt(AscW(timeStr(7)))
    
    
    
            Return New DateTime(yr + 2000, mo, dy, hr, mm, sc)
    
        End Function
    
    End Class
    

    Preview:

    I'm very gladful for your help with this... Thank You


    RohanD

    Monday, May 20, 2019 7:04 AM

Answers


  • Questions: 

    1) I need to add 5 hours, 29 minutes and 59 seconds to internet time

    2) Change windows 7/10 Date and Time with Internet Time


    Hi,

    add 5 hours, 29 minutes and 59 seconds to internet time

    GetNISTDateTime().AddHours(5).AddMinutes(29).AddSeconds(59)

    Change windows 7/10 Date and Time with Internet Time,try the following way:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/82a38ecb-5c28-42e6-aba2-9b36369680e0/changing-system-date-and-time-using-api-setsystemtime-in-vbnet?forum=vbgeneral

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 20, 2019 8:13 AM
    Moderator
  • Sir,

    Please tell me how to remove seconds from GetNISTTime 


    RohanD

    datetime format code:

    .ToString("yyyy-MM-dd HH:mm")

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 22, 2019 1:34 AM
    Moderator

All replies


  • Questions: 

    1) I need to add 5 hours, 29 minutes and 59 seconds to internet time

    2) Change windows 7/10 Date and Time with Internet Time


    Hi,

    add 5 hours, 29 minutes and 59 seconds to internet time

    GetNISTDateTime().AddHours(5).AddMinutes(29).AddSeconds(59)

    Change windows 7/10 Date and Time with Internet Time,try the following way:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/82a38ecb-5c28-42e6-aba2-9b36369680e0/changing-system-date-and-time-using-api-setsystemtime-in-vbnet?forum=vbgeneral

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 20, 2019 8:13 AM
    Moderator
  • Thank you so much sir...

    1st solution works... thank you

    but how to call function correctly. can you please help me with this too?

    Code Form1: 

    Imports System.IO
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Runtime.InteropServices
    Imports System.Data
    Imports System.Text
    
    Public Class Form1
        Public Shared LastSysTime As DateTime
    
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
            Label1.Text = GetNISTTime("time.nist.gov").AddHours(5).AddMinutes(29).AddSeconds(59).ToString
            Label2.Text = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss tt")
        End Sub
    
        Public Function GetNISTTime(ByVal host As String) As DateTime
    
            Dim timeStr As String
    
            Try
                Dim reader As New StreamReader(New TcpClient(host, 13).GetStream)
                LastSysTime = DateTime.UtcNow()
                timeStr = reader.ReadToEnd()
                reader.Close()
            Catch ex As SocketException
                Label1.Text = ex.Message
            Catch ex As Exception
                Label1.Text = ex.Message
            End Try
    
            Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5))
            Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2))
            Dim mo As Integer = Integer.Parse(timeStr.Substring(10, 2))
            Dim dy As Integer = Integer.Parse(timeStr.Substring(13, 2))
            Dim hr As Integer = Integer.Parse(timeStr.Substring(16, 2))
            Dim mm As Integer = Integer.Parse(timeStr.Substring(19, 2))
            Dim sc As Integer = Integer.Parse(timeStr.Substring(22, 2))
            Dim Temp As Integer = CInt(AscW(timeStr(7)))
    
    
    
            Return New DateTime(yr + 2000, mo, dy, hr, mm, sc)
    
        End Function
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            SetDateTime.SetLocalTime(True)
        End Sub
    End Class

    Code Class(SetDateTime):

    Imports System.Runtime.InteropServices
    
    Public Class SetDateTime
        <StructLayout(LayoutKind.Sequential)> Private Structure SYSTEMTIME
            <MarshalAs(UnmanagedType.U2)> Public Year As Short
            <MarshalAs(UnmanagedType.U2)> Public Month As Short
            <MarshalAs(UnmanagedType.U2)> Public DayOfWeek As Short
            <MarshalAs(UnmanagedType.U2)> Public Day As Short
            <MarshalAs(UnmanagedType.U2)> Public Hour As Short
            <MarshalAs(UnmanagedType.U2)> Public Minute As Short
            <MarshalAs(UnmanagedType.U2)> Public Second As Short
            <MarshalAs(UnmanagedType.U2)> Public Milliseconds As Short
        End Structure
    
        Private Declare Function SetLocalTime Lib "kernel32.dll" (ByRef lpSystemTime As SYSTEMTIME) As Boolean
    
        Public Shared Function UpdateSystemTime(ByVal DateObject As Date) As Boolean
            Dim newDT As SYSTEMTIME
            'Populate the systemtime object with the information from the date object passed in
            With newDT
                .Day = DateObject.Day
                .DayOfWeek = DateObject.DayOfWeek
                .Hour = DateObject.Hour
                .Milliseconds = DateObject.Millisecond
                .Minute = DateObject.Minute
                .Second = DateObject.Second
                .Month = DateObject.Month
                .Year = DateObject.Year
            End With
    
            Dim blnResult As Boolean = SetLocalTime(newDT)
    
            Return blnResult
        End Function
    End Class
    


    Preview:

    RohanD



    Monday, May 20, 2019 7:13 PM
  • Hi,

    code:

    Imports System.IO
    
    Public Class Form1
        Private Structure SYSTEMTIME
            Dim wYear As Short
            Dim wMonth As Short
            Dim wDayOfWeek As Short
            Dim wDay As Short
            Dim wHour As Short
            Dim wMinute As Short
            Dim wSecond As Short
            Dim wMilliseconds As Short
        End Structure
        Private Declare Function SetSystemTime Lib "kernel32" (ByRef lpSystemTime As SYSTEMTIME) As Long
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim timeStru As SYSTEMTIME
            timeStru.wYear = GetNISTTime("time.nist.gov").AddHours(5).AddMinutes(29).AddSeconds(59).Year
            timeStru.wMonth = GetNISTTime("time.nist.gov").AddHours(5).AddMinutes(29).AddSeconds(59).Month
            timeStru.wDay = GetNISTTime("time.nist.gov").AddHours(5).AddMinutes(29).AddSeconds(59).Day
            timeStru.wHour = GetNISTTime("time.nist.gov").AddHours(5).AddMinutes(29).AddSeconds(59).Hour
            timeStru.wMinute = GetNISTTime("time.nist.gov").AddHours(5).AddMinutes(29).AddSeconds(59).Minute
            timeStru.wSecond = GetNISTTime("time.nist.gov").AddHours(5).AddMinutes(29).AddSeconds(59).Second
            SetSystemTime(timeStru)
        End Sub
    
    End Class
    Note :administrator privileges

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, May 21, 2019 5:40 AM
    Moderator
  • Thank you sir, 

    there is issue, i try to run as admin but not working.


    RohanD

    Tuesday, May 21, 2019 7:05 AM
  • Hi,

    I want to ask if I only get an error when using admin rights.

    Public Function GetNISTTime(ByVal host As String) As DateTime
    
            Dim timeStr As String
    
            Try
                Dim reader As New StreamReader(New TcpClient(host, 13).GetStream)
                LastSysTime = DateTime.UtcNow()
                timeStr = reader.ReadToEnd()
                reader.Close()
            Catch ex As SocketException
                Label1.Text = ex.Message
            Catch ex As Exception
                Label1.Text = ex.Message
            End Try
    
            Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5))
            Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2))
            Dim mo As Integer = Integer.Parse(timeStr.Substring(10, 2))
            Dim dy As Integer = Integer.Parse(timeStr.Substring(13, 2))
            Dim hr As Integer = Integer.Parse(timeStr.Substring(16, 2))
            Dim mm As Integer = Integer.Parse(timeStr.Substring(19, 2))
            Dim sc As Integer = Integer.Parse(timeStr.Substring(22, 2))
            Dim Temp As Integer = CInt(AscW(timeStr(7)))
    
    
    
            Return New DateTime(yr + 2000, mo, dy, hr, mm, sc)
    
        End Function

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 21, 2019 7:48 AM
    Moderator
  • Sir, 

    Admin or Non admin error is same as below. 

     

    Details:

    See the end of this message for details on invoking 
    just-in-time (JIT) debugging instead of this dialog box.
    
    ************** Exception Text **************
    System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string.
    Parameter name: startIndex
       at System.String.Substring(Int32 startIndex, Int32 length)
       at GetIntTime.Form1.GetNISTTime(String host) in D:\PROJECTS\VB.Net 2019 Projects\TEST PROJECTS\MY TEST\Get Internet Time\GetIntTime\GetIntTime\Form1.vb:line 31
       at GetIntTime.Form1.Button1_Click(Object sender, EventArgs e) in D:\PROJECTS\VB.Net 2019 Projects\TEST PROJECTS\MY TEST\Get Internet Time\GetIntTime\GetIntTime\Form1.vb:line 61
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    
    
    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.3416.0 built by: NET472REL1LAST_B
        CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
    ----------------------------------------
    GetIntTime
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0.0
        CodeBase: file:///D:/PROJECTS/VB.Net%202019%20Projects/TEST%20PROJECTS/MY%20TEST/Get%20Internet%20Time/GetIntTime/GetIntTime/bin/Debug/GetIntTime.exe
    ----------------------------------------
    Microsoft.VisualBasic
        Assembly Version: 10.0.0.0
        Win32 Version: 14.7.3056.0 built by: NET472REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualBasic/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
    ----------------------------------------
    System
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.3416.0 built by: NET472REL1LAST_B
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Core
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.3362.0 built by: NET472REL1LAST_C
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.3324.0 built by: NET472REL1LAST_C
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.3056.0 built by: NET472REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System.Configuration
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.3056.0 built by: NET472REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    ----------------------------------------
    System.Xml
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.3056.0 built by: NET472REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    System.Runtime.Remoting
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.3056.0 built by: NET472REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Remoting/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
    ----------------------------------------
    
    ************** JIT Debugging **************
    To enable just-in-time (JIT) debugging, the .config file for this
    application or computer (machine.config) must have the
    jitDebugging value set in the system.windows.forms section.
    The application must also be compiled with debugging
    enabled.
    
    For example:
    
    <configuration>
        <system.windows.forms jitDebugging="true" />
    </configuration>
    
    When JIT debugging is enabled, any unhandled exception
    will be sent to the JIT debugger registered on the computer
    rather than be handled by this dialog box.
    
    
    


    RohanD

    Tuesday, May 21, 2019 8:00 AM
  • Hi,

    I am running your code, sometimes it runs successfully, sometimes it gives an error, which is very strange.

    Label1.Text = GetNISTTime("time.nist.gov").ToString

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 21, 2019 9:15 AM
    Moderator
  • Thank you so much sir...

    RohanD

    Tuesday, May 21, 2019 9:57 AM
  • Sir,

    Please tell me how to remove seconds from GetNISTTime 


    RohanD

    Tuesday, May 21, 2019 9:29 PM
  • Sir,

    Please tell me how to remove seconds from GetNISTTime 


    RohanD

    datetime format code:

    .ToString("yyyy-MM-dd HH:mm")

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 22, 2019 1:34 AM
    Moderator
  • Thank you so much sir... Works 

    RohanD

    Wednesday, May 22, 2019 7:13 AM