重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
想直接转换是不行的,首先你可以将字符串按照时间的格式修改为“2014-05-12 15:07:12”
成都创新互联公司主营万安网站建设的网络公司,主营网站建设方案,成都app软件开发公司,万安h5微信小程序开发搭建,万安网站营销推广欢迎万安等地区企业咨询
然后就 可以使用datetime.tryparse(s,out d)进行转换了
Dim a As String
Dim b As Date
a = "2016-11-18"
b = CDate(a)
本例中最主要的就是CDate()函数,这个函数是用于把字符型变量转换成日期型变量,
字符型变量(本例中的a)如果不是标准的日期格式,请先用字符串函数处理成标准日期格式再用CDate函数进行转换,否则会报错
给你一个例子,里边包含了几种不同格式转换成标准的日期时间格式;
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
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
将输入的字符串用各种方法尝试转换为日期变量。然后对再将日期变量输出回textbox中。
这里要做的就是用try 配合 各种转换为日期变量的函数来得到一个日期结果。如果所有格式都无法转为日期,则可以提示用户无法转换 或是根本 不操作。