0

In my controller I have a httpget that requires the input of two dates (ideally without time, but should work either way).

 // GET: api/Shippingschedules/shipname
        [HttpGet("{startdate},{enddate}")]
        public async Task<ActionResult<Shippingschedule>> GetShippingSchedulesByDates(string startdate, string enddate)
        {
            CultureInfo ukCulture = new CultureInfo("en-GB");
            DateTime sDate = DateTime.Parse(startdate, ukCulture.DateTimeFormat);
            DateTime eDate = DateTime.Parse(enddate, ukCulture.DateTimeFormat);

            ActionResult<Shippingschedule> Shippingschedule = await _context.Shippingschedules.Where(
                x => x.StartDate >= sDate && x.EndDate <= eDate).FirstOrDefaultAsync();

            if (Shippingschedule == null)
            {
                return NotFound();
            }

            return Shippingschedule;
        }

When I debug into the sDate line, the program stops with error:

String '1%2F1%2F2022' was not recognized as a valid DateTime. at System.DateTime.Parse(String s, IFormatProvider provider)

How do I deserialise this into a real datetime object?

Thanks

2
  • Can you share the value of startdate and enddate? Commented Apr 13, 2022 at 6:36
  • @YiyiYou - anything in en-GB format such as 1/1/2022 or 31/1/2022 or 12-FEB-2022 23:45 or 10-MAR-2022 1:12:00 PM... all of those could be entered into the text field and should be convertable Commented Apr 13, 2022 at 23:21

1 Answer 1

1

try this

using System.Net;

 var startdateDecoded= WebUtility.UrlDecode(startdate);
var enddateDecoded= WebUtility.UrlDecode(enddate);

CultureInfo ukCulture = new CultureInfo("en-GB");
 DateTime sDate = DateTime.Parse(startdateDecoded, ukCulture.DateTimeFormat);

.....

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.