11

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. I'm using .NET 4.

int TheMonth =0;
int TheDay = 0;
int TheYear = 0;
DateTime NewDate;

var TheIncomingParam = Request.Params.Get("__EVENTARGUMENT").ToString();

char[] TheBreak = { '/' };
string[] TheOutput = TheIncomingParam.Split(TheBreak);

try { TheMonth = Convert.ToInt32(TheOutput[0]); }
catch { }

try { TheDay = Convert.ToInt32(TheOutput[1]); }
catch { }

try { TheYear = Convert.ToInt32(TheOutput[2]); }
catch { }

if (TheMonth!=0 && TheDay!=0 && TheYear!=0)
{
        try { NewDate = new DateTime(TheYear, TheMonth, TheDay); }
        catch { var NoDate = true; }
}
7
  • 44
    have a good pick-up line.
    – jerebear
    Feb 10, 2011 at 20:30
  • What version of asp.net are you using?
    – Vishal
    Feb 10, 2011 at 20:30
  • 11
    Start by asking her out. Feb 10, 2011 at 20:32
  • 8
    1: Be attractive. 2: Don't be unattractive. 3: Have money. Feb 10, 2011 at 23:55
  • 1
    @T.E.D. Or, maybe not, LOL. He might have gotten more attention by the title ;-)
    – Josh
    Feb 11, 2011 at 16:03

4 Answers 4

13

Use one of the Parse methods defined on the DateTime structure.

These will throw an exception if the string is not parseable, so you may want to use one of the TryParse methods instead (not as pretty - they require an out parameter, but are safer):

DateTime myDate;
if(DateTime.TryParse(dateString, 
                  CultureInfo.InvariantCulture, 
                  DateTimeStyles.None, 
                  out myDate))
{
   // Use myDate here, since it parsed successfully
}

If you know the exact format of the passed in date, you can try using the ParseExact or TryParseExact that take date and time format strings (standard or custom) when trying to parse the date string.

3
  • Ah! It's already built into the framework! Thanks. BTW, I got my page to work: instead of page methods to send a parameter, I got it to work with __doPostBack.
    – frenchie
    Feb 10, 2011 at 20:39
  • @frenchie - Glad to hear you got it sorted... and the BCL has loads of such goodies built in. Spend some time reading the documentation on MSDN, you never know what you will find.
    – Oded
    Feb 10, 2011 at 20:42
  • Yes, there's a lot built-in that can help, but only if you know it exists in the first place. The other day I discovered extension methods and these are awesome!!
    – frenchie
    Feb 10, 2011 at 20:44
2

How about DateTime.TryParse and DateTime.TryParseExact?

The first one uses the current cultures date format.

1

.NET gives us a datetime.parse

http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

and a datetime.tryparse

http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx

which both are a good way to parse dates from strings

0
0

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;
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.