新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Sub gettime()
成都创新互联自2013年创立以来,是专业互联网技术服务公司,拥有项目成都网站建设、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元雁山做网站,已为上家服务,为雁山各地企业和个人服务,联系电话:18982081108
nowtime = Format(DateTime.Now, "yyyy/MM/dd hh:mm:ss")
MsgBox nowtime
End Sub
其中DateTime.Now为当前系统的时间
Format是设置时间格式
效果如下
pre class="html" name="code" /prepre class="html" name="code"Imports System.Runtime.InteropServices
StructLayout(LayoutKind.Sequential) _
Public Structure SYSTEMTIME
Public Year As Short
Public Month As Short
Public DayOfWeek As Short
Public Day As Short
Public Hour As Short
Public Minute As Short
Public Second As Short
Public Miliseconds As Short
End Structure
'api函数声明
DllImport("kernel32.dll", CharSet:=CharSet.Ansi) _
Public Shared Function SetSystemTime(ByRef time As SYSTEMTIME) As Boolean
End Function
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ServerTime As Date = F.G("select getdate()")
ServerTime = ServerTime.AddHours(-8)
Dim t As New SYSTEMTIME()
t.Year = ServerTime.Year()
t.Month = ServerTime.Month()
t.Day = ServerTime.Day
t.Hour = ServerTime.Hour
'这个函数使用的是0时区的时间,对于我们用+8时区的,时间要自己算一下.如要设12点,则为12-8
t.Minute = ServerTime.Minute
t.Second = ServerTime.Second
t.Miliseconds = ServerTime.Millisecond
Dim v As Boolean = SetSystemTime(t)
MessageBox.Show(v)
End Sub/prebr
pre/pre
pre class="vb" name="code" /prepre class="vb" name="code" /prepre class="vb" name="code" /pre
你是指Internet上的国际标准时间吗。
先加入控件 AxWinsock1 在.Com中Microsoft.Winsock
Public Class Form1
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)
Dim NoSrv As Boolean
Dim TimeFromNet As String
Dim OldTime As Date
Dim NewTime As Date
Dim MyDate As Date
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If AxWinsock1.CtlState MSWinsockLib.StateConstants.sckClosed Then AxWinsock1.Close()
AxWinsock1.Protocol = MSWinsockLib.ProtocolConstants.sckTCPProtocol
NetTime("") '首先取中科院国家授时中心时间
If NoSrv Or TimeFromNet = "" Then
NetTime("time.nist.gov") '取美国标准技时院时间
If NoSrv Or TimeFromNet = "" Then
MsgBox("检测不到网络标准时间服务器time.nist.gov!")
Else
NetTime("time.nist.gov")
If TimeFromNet = "" Then
MsgBox("网络标准时间服务器time.nist.gov超时!")
Else
MyDate = Mid(TimeFromNet, 8, 8)
OldTime = Mid(TimeFromNet, 17, 8)
NewTime = TimeSerial((Hour(OldTime) + 8) Mod 24, Minute(OldTime), Second(OldTime))
Dim MyTime As DateTime = MyDate Space(1) NewTime
MsgBox(MyTime)
'SetWindowsClock(MyTime)
End If
End If
Else
'使网络误差时间小,第2次再中科院国家授时中心时间
NetTime("")
If TimeFromNet = "" Then
MsgBox("网络标准时间服务器超时!")
Else
MyDate = Mid(TimeFromNet, 8, 8)
NewTime = Mid(TimeFromNet, 17, 8)
Dim MyTime As DateTime = MyDate Space(1) NewTime
MsgBox(MyTime)
'SetWindowsClock(MyTime)
End If
End If
End Sub
Private Sub NetTime(ByVal TimeSrv As String)
NoSrv = False
TimeFromNet = ""
If AxWinsock1.CtlState MSWinsockLib.StateConstants.sckClosed Then AxWinsock1.Close()
AxWinsock1.RemoteHost = TimeSrv ' "" 或 "time.nist.gov"
AxWinsock1.RemotePort = 13
AxWinsock1.LocalPort = 0
AxWinsock1.Connect()
Do While TimeFromNet = ""
If NoSrv Then Exit Do
Sleep(55)
System.Windows.Forms.Application.DoEvents()
Loop
If AxWinsock1.CtlState MSWinsockLib.StateConstants.sckClosed Then AxWinsock1.Close()
End Sub
Private Sub AxAxWinsock1_CloseEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles AxWinsock1.CloseEvent
AxWinsock1.Close()
End Sub
Private Sub AxAxWinsock1_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles AxWinsock1.DataArrival
AxWinsock1.GetData(TimeFromNet)
End Sub
Private Sub AxAxWinsock1_Error(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent) Handles AxWinsock1.Error
NoSrv = True
End Sub
End Class