验证 vb.net 中的日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24841806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate the date format in vb.net
提问by Piyush
I have the grid cell value to validate for a correct format as below
我有网格单元格值来验证正确的格式,如下所示
Date value should be in DD-MON-YYYY
format and for this i am using below validation
日期值应为DD-MON-YYYY
格式,为此我使用以下验证
Public Function ValidateDateForError(ByVal checkInputValue As String) As Boolean
Dim returnError As Boolean
Dim dateVal As DateTime
If Date.TryParseExact(checkInputValue, "DD-MON-YYYY",
System.Globalization.CultureInfo.CurrentCulture,
DateTimeStyles.None, dateVal) Then
returnError = True
Else
MessageBox.Show("not converted")
End If
Return returnError
End Function`
DateTime value should be in DD-MON-YYYY HH:MI:SS
format and for this i am using below validation
日期时间值应该是DD-MON-YYYY HH:MI:SS
格式,为此我使用以下验证
Public Function ValidateDateTimeForError(ByVal checkInputValue As String) As Boolean
Dim returnError As Boolean
Dim dateVal As DateTime
If DateTime.TryParseExact(checkInputValue, "DD-MON-YYYY HH:MI:SS",
System.Globalization.CultureInfo.CurrentCulture,
DateTimeStyles.None, dateVal) Then
returnError = True
End If
Return returnError
End Function`
EDate (valid European date) value should be in DD/MM/YY
format and for this i am using below validation
EDate(有效的欧洲日期)值应采用DD/MM/YY
格式,为此我使用以下验证
Public Function ValidateEDateForError(ByVal checkInputValue As String) As Boolean
Dim returnError As Boolean
Dim dateVal As Date
If Date.TryParseExact(checkInputValue, "DD/MM/YY",
System.Globalization.CultureInfo.CurrentCulture,
DateTimeStyles.None, dateVal) Then
returnError = True
End If
Return returnError
End Function`
JDate (valid Julian date) value should be in MM/DD/YY
format and for this i am using below validation
JDate(有效的儒略日期)值应采用MM/DD/YY
格式,为此我使用以下验证
Public Function ValidateJDateForError(ByVal checkInputValue As String) As Boolean
Dim returnError As Boolean
Dim dateVal As Date
If Date.TryParseExact(checkInputValue, "MM/DD/YY",
System.Globalization.CultureInfo.CurrentCulture,
DateTimeStyles.None, dateVal) Then
returnError = True
End If
Return returnError
End Function`
but none of above is working. could anyone tell me where i am making mistake?
但以上都不起作用。谁能告诉我我哪里出错了?
Thanks in Advance.
提前致谢。
回答by ??ssa P?ngj?rdenlarp
Using ParseExact
means that you will be telling it the precise format the string will be in. These are case sensitive to allow things like H
vs h
for 12/24 clock and MM
vs mm
to distinguish Month from Minutes. So, "DD-MON-YYYY" is invalid, "dd-MM-yyyy" may be what you are after.
使用ParseExact
意味着您将告诉它字符串将采用的精确格式。这些区分大小写以允许诸如H
vs h
12/24 时钟和MM
vsmm
区分月份和分钟。所以,“DD-MON-YYYY”是无效的,“dd-MM-yyyy”可能就是你想要的。
But, this means the user would always have to enter 2 digits for the day and month as in "19-07-2014" or "04-07-2014" which they are often not inclined to do, and seems harsh to impose. TryParseExact
will take an array of formats so you can be flexible:
但是,这意味着用户总是必须输入 2 位数字来表示日期和月份,如“19-07-2014”或“04-07-2014”,他们通常不愿意这样做,而且似乎很难强加。 TryParseExact
将采用一系列格式,因此您可以灵活:
Dim strFoo As String = "19-7-2014" ' d-MM-yyyy
Dim strBar As String = "19-07-2014" ' dd-MM-yyyy
Dim strJuly4 As String = "4-7-2014" ' d-M-yyyy
' several possible format styles
Dim formats() As String = {"d-MM-yyyy", "dd-MM-yyyy",
"dd-M-yyyy", "d-M-yyyy"}
Dim thisDt As DateTime
' this should work with all 3 strings above
If DateTime.TryParseExact(strFoo, formats,
Globalization.CultureInfo.InvariantCulture,
DateTimeStyles.None, thisDt) Then
Console.WriteLine("Success! {0}", thisDt.ToString)
End If
Most of the time there it is silly to force them to enter "04" for a month or day and d/M/yyyy
will work with strings with the leading "0" (but not the reverse!). Its is added here mainly to show how to pass an array of format patterns.
大多数情况下,强迫他们在一个月或一天内输入“04”并d/M/yyyy
使用以“0”开头的字符串(但不是相反!)是愚蠢的。在这里添加它主要是为了展示如何传递格式模式数组。
DOconsult MSDN for the proper Standard Date and Time Format Strings
DO查阅MSDN的适当标准日期和时间格式字符串
As for Julian Dates, oneform of them is to use the form yyDDD
to denote the 2 digit year and day of year for the last 3 digits. I dont know if the convention for this changed after 1/1/2000 when all Julian for this Century would sort below those for the 1990s. Still, here is how it is done:
至于儒略日期,其中一种形式是使用形式yyDDD
来表示最后 3 位数字的 2 位年份和年份。我不知道在 2000 年 1 月 1 日之后,当本世纪的所有朱利安都低于 1990 年代的惯例时,这一惯例是否有所改变。不过,这是如何完成的:
Dim jdt As DateTime = #2/11/2010#
Dim jdate As String = xdt.Year.ToString & xdt.DayOfYear.ToString("000")
' ===> '2010042 or 42nd day of 2010
jdate = (xdt.Year - 2000).ToString("00") & xdt.DayOfYear.ToString("000")
' ===> '10042 or 42nd day of 2010