How to make DateTime.TryParse() fail if no year is specified?

前端 未结 3 2213
难免孤独
难免孤独 2021-02-10 13:48

Consider the following code to parse a date. (Note that I\'m in the EN-gb locale):

const DateTimeStyles DATE_TIME_STYLES = DateTimeStyles.NoCurrentDateDefault |          


        
3条回答
  •  渐次进展
    2021-02-10 14:44

    You can use parse exact like this and catch the exception.

    CurrentUICulture.DateTimeFormat.ShortDatePattern will give you the cultures short date pattern.

    There is also DateTime.TryParseExact

    DateTime.ParseExact(value.ToString(), cultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern.ToString, cultureInfo.CurrentUICulture)
    

提交回复
热议问题