[英]DateTime object not accepting dd/MM/yyyy format ASP.NET Core Web API and C#
I have a DateTime
query parameter in an ASP.NET Core Web API, if I pass it a date with dd/MM/yyyy
format, I get an error "invalid value".我在 ASP.NET Core Web API 中有一个
DateTime
查询参数,如果我向它传递一个dd/MM/yyyy
格式的日期,我会收到错误“无效值”。
Actually, on the front end, I am using Blazor WASM and it's sending the value in the dd/MM/yyyy
format.实际上,在前端,我使用的是 Blazor WASM,它以
dd/MM/yyyy
格式发送值。
For now, I have to use string type and keep it formatted as MM/dd/yyyy
or yyyy/MM/dd
for API to accept.现在,我必须使用字符串类型并将其格式化为
MM/dd/yyyy
或yyyy/MM/dd
以便 API 接受。 is there any other solution?还有其他解决办法吗? I Want to use DateTime and not string can i get
yyyy/MM/dd
format from DateTime without converting it to string?我想使用 DateTime 而不是字符串 我可以从 DateTime 获取
yyyy/MM/dd
格式而不将其转换为字符串吗?
API Endpoint API 端点
[HttpGet]
public async Task<IActionResult> Get([FromQuery] DateTime date)
{
return Ok(date);
}
You can use long data type for the DateTime exchange.您可以将长数据类型用于 DateTime 交换。 This is Unix time which is UTC.
这是 UTC 时间 Unix。 Easy to exchange DateTime between server and client.
易于在服务器和客户端之间交换 DateTime。
From the javascript client-side you can get Unix time like this.从 javascript 客户端,您可以像这样获得 Unix 时间。
var d = new Date();
var ut = d.getTime();
From the asp.net server来自 asp.net 服务器
var ut = DateTimeOffset.Now.ToUnixTimeMilliseconds();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.