Best way to get a date with .NET?

前端 未结 4 1756
太阳男子
太阳男子 2021-02-20 07:56

I\'m getting a string back from my page and I want to make sure it\'s a date. This is what I have so far (it works) and I just want to know if this is the \"best\" way to do it.

4条回答
  •  你的背包
    2021-02-20 08:09

    I would just TryParse the input string:

        private bool ParseDateString()
        {
            var theIncomingParam = Request.Params.Get("__EVENTARGUMENT").ToString(); 
    
            DateTime myDate;
    
            if (DateTime.TryParse(theIncomingParam, CultureInfo.InvariantCulture, DateTimeStyles.None, out myDate))
            {
                int TheMonth = myDate.Month;
                int TheDay = myDate.Day;
                int TheYear = myDate.Year;
    
                // TODO: further processing of the values just read
    
                return true;
            }
            else
            {
                return false;
            }
        }
    

提交回复
热议问题