首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

VB 时间大有关问题

2012-02-13 
VB 时间大问题VB如果得到时间格式是这样的呢?例:1169606370用PHP中用time来得到的时间一样,但在VB中如果实

VB 时间大问题
VB   如果得到时间格式是这样的呢?
例:1169606370  
用PHP中用time来得到的时间一样,但在VB中如果实理呢?


[解决办法]
正好前几天遇到这个问题,用C编写了一个转换UTC的程序,翻译成VB:

Private Function UTCtoDateTime(lngSeconds As Long) As String
Dim nYear As Long, nMonth As Long, nDay As Long, nHour As Long, nMin As Long, nSec As Long

' ' '取得年份
nYear = 1970
Do While (lngSeconds > = GetYearSeconds(nYear))
lngSeconds = lngSeconds - GetYearSeconds(nYear)
nYear = nYear + 1
Loop
' ' '取得月份
nMonth = 1
Do While (lngSeconds > = GetMonthSeconds(nYear, nMonth))
lngSeconds = lngSeconds - GetMonthSeconds(nYear, nMonth)
nMonth = nMonth + 1
Loop

nDay = lngSeconds \ (3600# * 24) + 1
lngSeconds = lngSeconds Mod (3600# * 24)

nHour = lngSeconds \ (3600#)
lngSeconds = lngSeconds Mod 3600#

nMin = lngSeconds \ 60#
lngSeconds = lngSeconds Mod 60

nSec = lngSeconds


UTCtoDateTime = CStr(nYear) & "- " & CStr(nMonth) & "- " & CStr(nDay) & " " & CStr(nHour) & ": " & CStr(nMin) & ": " & CStr(nSec)
UTCtoDateTime = Format(UTCtoDateTime, "yyyy-mm-dd hh:nn:ss ")
End Function

' ' '将现在时刻转化成自1970-01-01 00:00:00经过的秒数
Private Function NowToUTC(tTime As Date) As Long
Dim nYear As Long, nMonth As Long
Dim lngSenconds As Long

lngSenconds = 0
For nYear = 1970 To year(tTime) - 1
lngSenconds = lngSenconds + GetYearSeconds(nYear)
Next

For nMonth = 1 To month(tTime) - 1
lngSenconds = lngSenconds + GetMonthSeconds(nYear, nMonth)
Next

lngSenconds = lngSenconds + 3600# * 24 * (Day(tTime) - 1)
lngSenconds = lngSenconds + 3600# * Hour(tTime)
lngSenconds = lngSenconds + 60 * Minute(tTime)
lngSenconds = lngSenconds + Second(tTime)

NowToUTC = lngSenconds
End Function


Private Function GetYearSeconds(year As Long) As Long
If year Mod 400 = 0 Or (year Mod 4 = 0 And year Mod 100 <> 0) Then
GetYearSeconds = 366# * 3600# * 24#
Else
GetYearSeconds = 365# * 3600# * 24#
End If
End Function

Private Function GetMonthSeconds(year As Long, month As Long) As Long
Select Case month
Case 1, 3, 5, 7, 8, 10, 12
GetMonthSeconds = 3600# * 24 * 31
Case 4, 6, 9, 11
GetMonthSeconds = 3600# * 24 * 30
Case 2
If year Mod 400 = 0 Or (year Mod 4 = 0 And year Mod 100 <> 0) Then
GetMonthSeconds = 3600# * 24 * 29
Else
GetMonthSeconds = 3600# * 24 * 28
End If
End Select
End Function

Private Sub Command1_Click()
Dim i As Long
Dim t As Date
t = Now
i = NowToUTC(t)
Debug.Print "Now: "; t
Debug.Print "UTC: "; CStr(i)
Debug.Print "Time: "; UTCtoDateTime(i)
End Sub

运行结果:
Now: 2007-1-25 12:55:19
UTC: 1169729719
Time:2007-01-25 12:55:19

热点排行