我对vb 2008和MySQL服务器中的日期格式设置有一些问题。 我想有一个输入接受这种格式dd-mm-yyyy和dd/mm/yyyy然后将其转换为mysql日期格式yyyy-mm-dd的日期的文本框。在VB 2008和MySQL中的日期格式设置
到目前为止,我不知道如何开始这种日期格式,但我想在一个模块中。
任何想法非常赞赏。谢谢
我对vb 2008和MySQL服务器中的日期格式设置有一些问题。 我想有一个输入接受这种格式dd-mm-yyyy和dd/mm/yyyy然后将其转换为mysql日期格式yyyy-mm-dd的日期的文本框。在VB 2008和MySQL中的日期格式设置
到目前为止,我不知道如何开始这种日期格式,但我想在一个模块中。
任何想法非常赞赏。谢谢
用户有各种格式输入日期的倾向,如dd-MM-yyyy,dd/M/yyyy,d.MM.yyyy等。尽管我们可以使用验证器来抑制它们,但是正则表达式)等但没有使用任何正则表达式,验证器和插件,我们可以使用下面的方法来验证和转换为字符串传递日期的格式如下:
day[separator]month[separator]year
哪天可以在d
或dd
(3,03),
月可以是M
或MM
(7,07),
年可以是yy
或yyyy
(15,2015)和
分离器可以是[space]
,/
,.
或-
,并且可以是混合格式,如
dd-MM-YYYY
,dd/MM/YYYY
,dd.MM.YYYY
,dd MM yyyy
,d-M-YY
,d/M/YY
,d.M.YY
,d M yy
,d-MM-YYYY
, dd/M/YYYY
,dd.MM.YY
等
该函数将date作为上述任何格式的字符串参数,并在yyyy-MM-dd
中验证并返回一个日期。它检查闰年并以1970年为基础来检查从yy
到yyyy
格式的转换。
Public Function DMYtoYMD(stDate As String) As String
Dim blValidDate As Boolean = True
While stDate.Contains(" ")
stDate = stDate.Replace(" ", " ")
End While
stDate = stDate.Trim.Replace(" ", "-").Replace("/", "-").Replace(".", "-")
Dim stFinalDate As String = ""
If stDate.Length > 0 Then
Dim mc As String() = stDate.Split(CChar("-"))
Dim inDay As Integer = CInt(mc(0))
Dim inMonth As Integer = 0
If IsNumeric(mc(1)) Then
inMonth = CInt(mc(1))
Else
For inMonthNo As Integer = 1 To 13
If inMonthNo = 13 Then
inMonth = 0
blValidDate = False
Return ""
ElseIf MonthName(inMonthNo, True).ToLower = mc(1).ToLower.Substring(0, 3) Then
inMonth = inMonthNo
Exit For
End If
Next
End If
Dim inYear As Integer = Math.Abs(CInt(mc(2)))
stFinalDate = ""
If inYear < 100 Then
'use above condition to convert 0 (i.e. 2000) to current year to 20xx and all others to 19xx
'If inYear >= (CInt(Format(Today, "yy")) + 1) Then
'use this condition to convert all yy year above 70 to 19xx and all others to 20xx
If inYear > 70 Then
inYear += 1900
Else
inYear += 2000
End If
'ignoring year from 101 to 999 (as per my specific requirement for the project). valid dates are to be from 1900 or above
ElseIf inYear < 1900 Then '101 to 999
stFinalDate = ""
blValidDate = False
End If
If (inMonth < 1 OrElse inMonth > 12) Then
stFinalDate = ""
blValidDate = False
ElseIf (inDay < 1 OrElse inDay > 31) Then
stFinalDate = ""
blValidDate = False
ElseIf ((inMonth = 4 OrElse inMonth = 6 OrElse inMonth = 9 OrElse inMonth = 11) AndAlso inDay = 31) Then
stFinalDate = ""
blValidDate = False
ElseIf (inMonth = 2) Then
Dim isleap As Boolean = (inYear Mod 4 = 0 AndAlso (inYear Mod 100 <> 0 OrElse inYear Mod 400 = 0))
If (inDay > 29 OrElse (inDay = 29 AndAlso Not isleap)) Then
stFinalDate = ""
blValidDate = False
End If
End If
If blValidDate Then
stFinalDate = CStr(New Date(inYear, inMonth, inDay))
If Not IsDate(stFinalDate) Then
stFinalDate = ""
Else
stFinalDate = Format(New Date(inYear, inMonth, inDay), "yyyy-MM-dd")
End If
End If
End If
Return stFinalDate
End Function
你可以简单地把它作为
Dim dtDate As String = ""
dtDate = DMYtoYMD(TextBox1.Text) 'or as Class1.DMYtoYDM as per your code structure
如果输入的日期是无效的该函数返回一个零长度字符串。它可以进一步修改为包括MMM
或MMMM
格式以转换为MM
,并根据您的要求检查MM-dd-yyyy
或其他输入格式。