由日期挑选器返回日期字符串的代码,日期串分隔符可自定义
两个函数的功能都是通过一个日期挑选器dtpicker,得到形如"2007/05/12"这种格式的日期。
我写了两个,大家看看哪个更好些。呵呵 '--------------------------------------
'返回日期挑选器控件的日期
'参数:日期挑选器
'返回格式:yyyy/mm/dd[分隔符可设定]
'创建日期:2007-8-21
'修改日期:2007-8-21
'--------------------------------------
Public Function getDtDate(ByRef dt As DTPicker, Optional ByVal vstrSplit As String = "/") As String
On Error GoTo errhandler
Dim strdate As String * 10
strdate = Format(dt.Value, "yyyy-mm-dd")
getDtDate = Replace(strdate, "-", vstrSplit)
Exit Function
errhandler:
getDtDate = ""
End Function
'--------------------------------------
'返回日期挑选器控件的日期
'参数:日期挑选器
'返回格式:yyyy/mm/dd[分隔符可设定]
'创建日期:2007-8-21
'修改日期:2007-8-21
'--------------------------------------
Public Function getDtDate2(ByRef dt As DTPicker, Optional ByVal vstrSplit As String = "/") As String
On Error GoTo errhandler
Dim strYear As String * 4, strMonth As String * 2, strDay As String * 2
strYear = Trim(dt.Year)
If dt.Month <= 9 Then
strMonth = "0" & Trim(dt.Month)
Else
strMonth = Trim(dt.Month)
End If
If dt.Day <= 9 Then
strDay = "0" & Trim(dt.Day)
Else
strDay = Trim(dt.Day)
End If
getDtDate2 = strYear & vstrSplit & strMonth & vstrSplit & strDay
Exit Function
errhandler:
getDtDate2 = ""
End Function
两个函数的功能都是通过一个日期挑选器dtpicker,得到形如"2007/05/12"这种格式的日期。
我写了两个,大家看看哪个更好些。呵呵 '--------------------------------------
'返回日期挑选器控件的日期
'参数:日期挑选器
'返回格式:yyyy/mm/dd[分隔符可设定]
'创建日期:2007-8-21
'修改日期:2007-8-21
'--------------------------------------
Public Function getDtDate(ByRef dt As DTPicker, Optional ByVal vstrSplit As String = "/") As String
On Error GoTo errhandler
Dim strdate As String * 10
strdate = Format(dt.Value, "yyyy-mm-dd")
getDtDate = Replace(strdate, "-", vstrSplit)
Exit Function
errhandler:
getDtDate = ""
End Function
'--------------------------------------
'返回日期挑选器控件的日期
'参数:日期挑选器
'返回格式:yyyy/mm/dd[分隔符可设定]
'创建日期:2007-8-21
'修改日期:2007-8-21
'--------------------------------------
Public Function getDtDate2(ByRef dt As DTPicker, Optional ByVal vstrSplit As String = "/") As String
On Error GoTo errhandler
Dim strYear As String * 4, strMonth As String * 2, strDay As String * 2
strYear = Trim(dt.Year)
If dt.Month <= 9 Then
strMonth = "0" & Trim(dt.Month)
Else
strMonth = Trim(dt.Month)
End If
If dt.Day <= 9 Then
strDay = "0" & Trim(dt.Day)
Else
strDay = Trim(dt.Day)
End If
getDtDate2 = strYear & vstrSplit & strMonth & vstrSplit & strDay
Exit Function
errhandler:
getDtDate2 = ""
End Function
---------------------
日期挑选器呀.
http://vbsdn.vicp.net/
http://vbbbs.eicp.net/
----------------
谢谢,真是好办法,请教一下,
"yyyy\/mm\/dd"中的两个"\"起什么作用呀,是转义吗?
哪有这种格式的说明呀?
要写成format(a,"yyyy\/mm\/dd")
就行了.
下面是msdn上的说明. (/) 日期分隔符。在一些国别,可能用其他符号来当日期分隔符。在格式化日期数值时,日期分隔符可以用来分隔年、月、日。日期分隔符的真正字符在格式输出时,取决于系统设置。 (\) 将格式字符串中下一个字符显示出来。如想显示一作为原义字符并有特殊含义的字符,可将此字符置于反斜杠 (\) 之后。反斜杠本身并不会显示出来。而使用反斜杠 (\) 的效果和使用双引号是一样的。如想显示反斜杠字符,可使用两个反斜杠 (\\)。
那些不能显示为原义字符的字符是日期格式字符和时间格式字符(a, c, d, h, m, n, p, q, s, t, w, y, / 和 :)、数值格式字符(#, 0, %, E, e, 逗点和句点)和字符串格式字符(@, &, <, > 和 !)。