vb.net中怎样获取日期中的年月日时分秒
Dim dateTime As String = "2001/12/23"
此时用CDate()函数将其转化为日期格式,然后通过CDate(dateTime).year/CDate(dateTime).month/CDate(dateTime).day
可以获得其年月日,但当加上时间时如:
Dim dateTime As String = "2001/12/23 12:25:33"
此时用CDate()函数会报错,说无法转化成时间类型
那么Date类型的hour属性应该怎么获取?
[解决办法]
是不是因为"2001/12/23 12:25:33"里的冒号你写成全角了啊
[解决办法]
要在英文输入法状态下输入冒号
Dim dateTime As String = "2001/12/23 12:25:33"
[解决办法]
Dim dateTime As String = "2001/12/23 12:25:33"
MsgBox(CDate(dateTime).Year & " " & CDate(dateTime).Month & " " & CDate(dateTime).Day)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strMyDateTime As String
strMyDateTime = "2001/12/23 12:25:33"
Dim myDateTime As System.DateTime
'myDateTime = System.DateTime.Parse(strMyDateTime, "yyyy/MM/dd hh:mm:ss")
myDateTime = System.DateTime.Parse(strMyDateTime)
'Dim moment As New System.DateTime(1999, 1, 13, 3, 57, 32, 11)
' Year gets 1999.
Dim year As Integer = myDateTime.Year
' Month gets 1 (January).
Dim month As Integer = myDateTime.Month
' Day gets 13.
Dim day As Integer = myDateTime.Day
' Hour gets 3.
Dim hour As Integer = myDateTime.Hour
' Minute gets 57.
Dim minute As Integer = myDateTime.Minute
' Second gets 32.
Dim second As Integer = myDateTime.Second
' Millisecond gets 11.
Dim millisecond As Integer = myDateTime.Millisecond
TextBox1.Text = myDateTime.ToString
End Sub
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dateTime As String = "2001/12/23"
Dim s As String = CDate(dateTime).Year.ToString & "/" & CDate(dateTime).Month.ToString & "/" & CDate(dateTime).Day.ToString & "/" & "12:25:33"
Dim T As Date = CDate("12:25:33")
dateTime = dateTime & " " & T
MsgBox(s)
MsgBox(dateTime)
End Sub
End Class