兩種方式自己選用
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比巴楚網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式巴楚網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋巴楚地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String
s = DateTime.Today.Year()
s = s DateTime.Today.Month()
s = s DateTime.Today.Day()
s = s DateTime.Now.Hour()
s = s DateTime.Now.Minute()
s = s DateTime.Now.Second()
MsgBox(s, vbDefaultButton1, Now())
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim s As String
s = Format(Now(), "yyyymmddhhmmss")
MsgBox(s, vbYes, Now())
End Sub
vb把數(shù)值轉(zhuǎn)化為時(shí)間格式:
VB.net 中 取系統(tǒng)時(shí)間
Dim datestr As String = ""
datestr = Format(Now(), "yyyy/MM/dd H:mm:ss ffff")
用戶定義的日期/時(shí)間格式(Format 函數(shù))
轉(zhuǎn)化代碼:
Dim t As Integer, t1 As Integer, t2 As Integer, s As String
Dim tim As Date
Dim i As Integer, j As Integer
Private Sub Command1_Click()
s = InputBox("分鐘數(shù):", "輸入", 67)
If s = "" Then Exit Sub
t = Val(s)
If t = 0 Then Exit Sub
t1 = t \ 60
t2 = t Mod 60
s = t1 ":" t2
tim = Format(s, "hh:mm:ss")
Text1.Text = tim
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim tt1 As Integer, tt2 As Integer, tt3 As Integer, tt As String
tt = Text1.Text
tt1 = Val(Left(tt, Len(tt) - 6))
tt2 = Val(Mid(tt, Len(tt) - 4, 2))
tt3 = Val(Right(tt, 2))
tt3 = tt3 - 1
If tt3 0 Then tt3 = 59: tt2 = tt2 - 1
If tt2 0 Then tt2 = 59: tt1 = tt1 - 1
If tt1 0 Then Timer1.Enabled = False: Exit Sub
tt = tt1 ":" tt2 ":" tt3
tim = Format(tt, "hh:mm:ss")
Text1.Text = tim
End Sub
Dim a As String
Dim b As Date
a = "2016-11-18"
b = CDate(a)
本例中最主要的就是CDate()函數(shù),這個(gè)函數(shù)是用于把字符型變量轉(zhuǎn)換成日期型變量,
字符型變量(本例中的a)如果不是標(biāo)準(zhǔn)的日期格式,請先用字符串函數(shù)處理成標(biāo)準(zhǔn)日期格式再用CDate函數(shù)進(jìn)行轉(zhuǎn)換,否則會(huì)報(bào)錯(cuò)
將輸入的字符串用各種方法嘗試轉(zhuǎn)換為日期變量。然后對(duì)再將日期變量輸出回textbox中。
這里要做的就是用try 配合 各種轉(zhuǎn)換為日期變量的函數(shù)來得到一個(gè)日期結(jié)果。如果所有格式都無法轉(zhuǎn)為日期,則可以提示用戶無法轉(zhuǎn)換 或是根本 不操作。
想直接轉(zhuǎn)換是不行的,首先你可以將字符串按照時(shí)間的格式修改為“2014-05-12 15:07:12”
然后就 可以使用datetime.tryparse(s,out d)進(jìn)行轉(zhuǎn)換了
給你一個(gè)例子,里邊包含了幾種不同格式轉(zhuǎn)換成標(biāo)準(zhǔn)的日期時(shí)間格式;
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
' 01/09/2001 00:00:00
Dim MyDateTime1 As DateTime = DateTime.Parse("Sep 2001")
' 05/09/2001 14:15:33
Dim MyDateTime2 As DateTime = DateTime.Parse("Wed 5 September 2001 14:15:33")
' 01/09/2005 00:00:00
Dim MyDateTime3 As DateTime = DateTime.Parse("5,9,01")
' 09/05/2001 14:15:33
Dim MyDateTime4 As DateTime = DateTime.Parse("5/9/2001 14:15:33")
' 當(dāng)前系統(tǒng)日期 14:15:00
Dim MyDateTime5 As DateTime = DateTime.Parse("2:15 PM")
Dim MyInfo As String = MyDateTime1.ToString()
MyInfo += vbCrLf + MyDateTime2.ToString()
MyInfo += vbCrLf + MyDateTime3.ToString()
MyInfo += vbCrLf + MyDateTime4.ToString()
MyInfo += vbCrLf + MyDateTime5.ToString()
MessageBox.Show(MyInfo, "信息提示", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.Message, "信息提示", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
End Class