Check if a string is a valid date using DateTime.TryParse

前端 未结 6 1985
梦谈多话
梦谈多话 2020-11-30 05:27

I am using DateTime.TryParse() function to check if a particular string is a valid datetime not depending on any cultures.
To my surprise , the function re

6条回答
  •  误落风尘
    2020-11-30 06:11

    Try using

    DateTime.ParseExact(
        txtPaymentSummaryBeginDate.Text.Trim(),
        "MM/dd/yyyy", 
        System.Globalization.CultureInfo.InvariantCulture
    );
    

    It throws an exception if the input string is not in proper format, so in the catch section you can return false;

提交回复
热议问题