VB.Net在线时间

时间:2019-11-22 06:58:13

标签: vb.net

我有一个返回在线日期和时间的函数,但是由于某些原因,分钟数不起作用。当我在分钟上添加一个值时,它始终保持不变,并且不会前进。

 Public Function OnlineTimeNow() As DateTime

        ' --- create instance of UDP
        Dim objSck As System.Net.Sockets.UdpClient
        Dim ipAny As System.Net.IPEndPoint = New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
        objSck = New System.Net.Sockets.UdpClient(ipAny)
        ' --- send UDP
        Dim sdat As Byte() = New Byte(47) {}
        sdat(0) = &HB
        objSck.Send(sdat, sdat.GetLength(0), "time.windows.com", 123)
        ' --- receive UDP
        Dim rdat As Byte() = objSck.Receive(ipAny)
        ' elapsed time (date and time) from 1900/01/01 --- 
        Dim elapsedTotalSec As Long ' elapsed seconds from 1900/01/01 --- 
        Dim Days As Long    ' days --- 日
        Dim HH As Long    ' hours --- 時
        Dim MM As Long    ' minutes --- 分
        Dim SS As Long    ' seconds --- 秒
        ' --- elapsed seconds from 1900/01/01 --- 
        elapsedTotalSec = CLng(
              rdat(40) * Math.Pow(2, (8 * 3)) +
              rdat(41) * Math.Pow(2, (8 * 2)) +
              rdat(42) * Math.Pow(2, (8 * 1)) +
              rdat(43))
        ' ---
        Days = elapsedTotalSec \ (24 * 60 * 60)   ' days 
        SS = elapsedTotalSec Mod (24 * 60 * 60) ' mod seconds 
        HH = SS \ (60 * 60)
        SS = SS Mod (60 * 60)
        MM = MM \ (60 * 60)
        SS = SS Mod 60
        ' --- convert to DateTime type 
        'Dim dtTime As DateTime = "1900/01/01"
        Dim dtTime As DateTime
        'dtTime = dtTime.AddDays(Days)
        dtTime = dtTime.AddHours(HH)
        dtTime = dtTime.AddMinutes(MM)
        dtTime = dtTime.AddSeconds(SS)
        ' --- change Greenwich Mean Time to local time (my sample is Japanese Time) 
        dtTime = dtTime.AddHours(8) ' <<-- *** please modify (n) according to your location ***
        ' ---
        Return dtTime

    End Function

2 个答案:

答案 0 :(得分:0)

如果您想获取GTM时间,只需使用:

Dim dtTime As DateTime
dtTime = DateTime.UtcNow

这是如何将Utc时间转换为另一个时区的示例:

 Sub Main()

        Dim DT As DateTime = DateTime.UtcNow    
        Dim DTSEAsia As DateTime
        Dim Tz As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("SE Asia Standard Time")

        DTSEAsia = TimeZoneInfo.ConvertTimeFromUtc(DT, Tz)

    End Sub

这是TimeZoneInfo

可能的值
Dateline Standard Time
UTC-11
Samoa tandard Time
Hawaiian Standard Time
Alaskan Standard Time
Pacific Standard Time (Mexico)
Pacific Standard Time
US Mountain Standard Time
Mountain Standard Time (Mexico)
Mountain Standard Time
Central America Standard Time
Central Standard Time
Central Standard Time (Mexico)
Canada Central Standard Time
SA Pacific Standard Time
Eastern Standard Time
US Eastern Standard Time
Venezuela Standard Time
Paraguay Standard Time
Atlantic Standard Time
Central Brazilian Standard Time
SA Western Standard Time
Pacific SA Standard Time
Newfoundland Standard Time
E.South America Standard Time
Argentina Standard Time
SA Eastern Standard Time
Greenland Standard Time
Montevideo Standard Time
UTC-2
Mid-Atlantic Standard Time
Azores Standard Time
Cape Verde Standard Time
Morocco Standard Time
UTC
GMT Standard Time
Greenwich Standard Time
W.Europe Standard Time
Central Europe Standard Time
Romance Standard Time
Central European Standard Time
W.Central Africa Standard Time
Namibia Standard Time
Jordan Standard Time
GTB Standard Time
Middle East Standard Time
Egypt Standard Time
Syria Standard Time
South Africa Standard Time
FLE Standard Time
Israel Standard Time
E.Europe Standard Time
Arabic Standard Time
Arab Standard Time
Russian Standard Time
E.Africa Standard Time
Iran Standard Time
Arabian Standard Time
Azerbaijan Standard Time
Mauritius Standard Time
Georgian Standard Time
Caucasus Standard Time
Afghanistan Standard Time
Ekaterinburg Standard Time
Pakistan Standard Time
West Asia Standard Time
India Standard Time
Sri Lanka Standard Time
Nepal Standard Time
Central Asia Standard Time
Bangladesh Standard Time
N.Central Asia Standard Time
Myanmar Standard Time
SE Asia Standard Time
North Asia Standard Time
China Standard Time
North Asia East Standard Time
Singapore Standard Time
W.Australia Standard Time
Taipei Standard Time
Ulaanbaatar Standard Time
Tokyo Standard Time
Korea Standard Time
Yakutsk Standard Time
Cen.Australia Standard Time
AUS Central Standard Time
E.Australia Standard Time
AUS Eastern Standard Time
West Pacific Standard Time
Tasmania Standard Time
Vladivostok Standard Time
Central Pacific Standard Time
New Zealand Standard Time
UTC+12
Fiji Standard Time
Kamchatka Standard Time
Tonga Standard Time

答案 1 :(得分:0)

只需为下一个用户“在线日期和时间VB.NET”发布一种简单的方法

Private Sub OnlineDateAndTime()

        Dim client As WebClient = New WebClient()

        'Download The TextFile From Web.
        'Refer To "https://worldtimeapi.org/" about the link below
        Dim DateAndTimeFromWeb = DateTimeOffset.Parse(client.DownloadString("https://worldtimeapi.org/api/timezone/Asia/Manila.txt").Split(ChrW(10)).Skip(2).First().Substring(10))


        Dim TimeNow As DateTime
        Dim DateNow As DateTime


        'Get Only Time
        TimeNow = DateAndTimeFromWeb.ToString
        TimeNow = TimeOfDay.ToString("h:mm:ss tt")

        'Get Only Date
        DateNow = DateAndTimeFromWeb.ToString
        DateNow = DateTime.Now.ToString("dd/MM/yyyy")

        'Show Results in Textbox or Label or Any
        TextBox1.Text = DateNow
        TextBox2.Text = TimeNow

    End Sub

@Jimi的积分-谢谢^ _ ^