十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
两种方式自己选用
成都创新互联-专业网站定制、快速模板网站建设、高性价比巴楚网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式巴楚网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖巴楚地区。费用合理售后完善,10年实体公司更值得信赖。
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把数值转化为时间格式:
VB.net 中 取系统时间
Dim datestr As String = ""
datestr = Format(Now(), "yyyy/MM/dd H:mm:ss ffff")
用户定义的日期/时间格式(Format 函数)
转化代码:
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("分钟数:", "输入", 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()函数,这个函数是用于把字符型变量转换成日期型变量,
字符型变量(本例中的a)如果不是标准的日期格式,请先用字符串函数处理成标准日期格式再用CDate函数进行转换,否则会报错
将输入的字符串用各种方法尝试转换为日期变量。然后对再将日期变量输出回textbox中。
这里要做的就是用try 配合 各种转换为日期变量的函数来得到一个日期结果。如果所有格式都无法转为日期,则可以提示用户无法转换 或是根本 不操作。
想直接转换是不行的,首先你可以将字符串按照时间的格式修改为“2014-05-12 15:07:12”
然后就 可以使用datetime.tryparse(s,out d)进行转换了
给你一个例子,里边包含了几种不同格式转换成标准的日期时间格式;
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")
' 当前系统日期 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