ASP.net Core 6 - 如何将序列化日期转换为 DateTime?
在我的控制器中,我有一个httpget,需要两个日期的输入(理想情况下,没有时间,但应使用任何一种方式)。
// 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;
}
当我调试sdate
行时,该程序停止了错误:
字符串'1%2F1%2F2022'未被认为是有效的日期时间。在 system.datetime.parse(字符串S,IformatProvider提供商)
如何将其归为真实dateTime
对象?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一下
try this