D-W
D-W

Reputation: 5341

DateTime Parameter Throws exception

When passing a parameter, sometimes I get the following error thrown

'String was not recognized as a valid DateTime'

dates like this it does not like:

(Chrome)

date=2017-03-28T13%3a01%3a59+01%3a00

date=2017-04-01T10%3a35%3a57+01%3a00

(Safari 10)

date=2017-06-02

public ActionResult Add(string date = null)
{
   DateTime startTime = DateTime.Now;
   if (date != null)
   {
      startTime = DateTime.Parse(date);
   }
}

Any ideas?

Upvotes: 2

Views: 140

Answers (1)

Boris Sokolov
Boris Sokolov

Reputation: 1793

You could try to unescape the string:

public ActionResult Add(string date = null)
{
    DateTime startTime = DateTime.Now;

    if (date != null)
    {
        startTime = DateTime.Parse(Uri.UnescapeDataString(date));
    }
}

Upvotes: 2

Related Questions