Convert a string to a date in .net

后端 未结 6 1563
忘了有多久
忘了有多久 2020-12-10 15:21

I\'m reading text from a flat file in c# and need to test whether certain values are dates. They could be in either YYYYMMDD format or MM/DD/YY format. What is the simplest

6条回答
  •  旧巷少年郎
    2020-12-10 15:48

    you could try also TryParseExact for set exact format. method, here's documentation: http://msdn.microsoft.com/en-us/library/ms131044.aspx

    e.g.

    DateTime outDt;
    bool blnYYYMMDD = 
         DateTime.TryParseExact(yourString,"yyyyMMdd"
                                ,CultureInfo.CurrentCulture,DateTimeStyles.None
                                , out outDt);
    

    I hope i help you.

提交回复
热议问题