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

前端 未结 3 1962
心在旅途
心在旅途 2021-02-10 14:14

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:39

    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)
    

提交回复
热议问题