十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
在你对日期/时间进行格式化时,控制面板中的地区与语言选项部分的设置会影响你所得到的结果。那些设置用来初始化DateTimeFormatInfo对象,这个对象与当前线程的文化有关,并提供控制格式的值。
成都创新互联从2013年成立,是专业互联网技术服务公司,拥有项目网站设计、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元大方做网站,已为上家服务,为大方各地企业和个人服务,联系电话:13518219792
Dim dateTimeInfo as DateTime = DateTime.Now
MessageBox.Show (dateTimeInfo)
Dim strMonth as String = dateTimeInfo.ToString("F")
MessageBox.Show(strMonth)
上面的代码定义了日期时间变量dateTimeInfo并将其值设为当前日期/时间。然后,我再定义字符串变量strMonth并将dateTimeInfo的值转换为"带长时间的完整日期/时间"格式下的字符串。
下面是一个标准日期格式说明符列表:
d:短日期
D:长日期
t:短时间
T:长时间
f:带短时间的完整日期/时间
F:带长时间的完整日期/时间
g:带短时间的一般日期/时间
G:带长时间的一般日期/时间
M或m:月-日
R或r:RFC1123
s:遵守ISO 8601的可分类日期/时间
u:国际可分类日期/时间
U:带长时间的完整日期/时间。(此格式与F相同,但它用于国际GMT时间。)
Y或y:年-月
Dim ThisDay As String = Format(Now, "yyyy-MM-dd") '获得当前日期字符串
Dim ThisDateTime As DateTime = Convert.ToDateTime(ThisDay) '当前日期转换成DateTime
Dim ThisWeekDay As Integer = ThisDateTime.DayOfWeek '获得当前日期是星期几
Dim differadd As Integer = 1 - ThisWeekDay '相差的天数(星期1与当前星期几相差的天数)
Dim MyAdd As New TimeSpan(differadd, 0, 0, 0)
Dim MyYear As Integer = Format(Now, "yyyy") '获取当前日期的年份
Dim MyMonth As Integer = Format(Now, "MM") '获取当前日期的月份
Dim MyDay As Integer = Format(Now, "dd") '获取当前日期是几号
Dim MyToday As DateTime = New DateTime(MyYear, MyMonth, MyDay)
Dim Yourday As DateTime = MyToday.Add(MyAdd)
MsgBox("本周星期一的日期是:" Yourday)
给你一个例子,里边包含了几种不同格式转换成标准的日期时间格式;
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
Dim a As String
Dim b As Date
a = "2016-11-18"
b = CDate(a)
本例中最主要的就是CDate()函数,这个函数是用于把字符型变量转换成日期型变量,
字符型变量(本例中的a)如果不是标准的日期格式,请先用字符串函数处理成标准日期格式再用CDate函数进行转换,否则会报错