简体   繁体   English

日期时间 object 不接受 dd/MM/yyyy 格式 ASP.NET 核心 Web API 和 C#

[英]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/yyyyyyyy/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.

相关问题 DateTime格式为YYYY-mm-dd转换c#asp.net - DateTime Format to YYYY-mm-dd conversion c# asp.net ASP.NET 核心 Web API - 如何在 Z336ZFF11E9AA6827A14A7A7A 上将默认日期格式设置为 yyyy-MM-dd - ASP.NET Core Web API - How to set default Date format as yyyy-MM-dd on Swagger 为什么DateTime.Now在asp.net c#中保存到SQL YYYY-DD-MM? - Why DateTime.Now save to SQL YYYY-DD-MM in asp.net c#? 在ASP.NET中发布后,DateTime.Now.ToString(“ dd / MM / yyyy”)以dd.mm.yyyy格式显示 - DateTime.Now.ToString(“dd/MM/yyyy”) shows in dd.mm.yyyy format after publishing in asp.net 保存格式为yyyy / MM / dd HH:mm:ss的datetime导致asp.net mvc中的验证错误 - saving datetime with format yyyy/MM/dd HH:mm:ss cause validation error in asp.net mvc 将日期时间显示为MM / dd / yyyy HH:mm格式c# - Display datetime into MM/dd/yyyy HH:mm format c# 在ASP.NET Mvc中将dd / MM / yyyy字符串格式日期转换为MM / dd / yyyy日期格式 - Converting dd/MM/yyyy string format date to MM/dd/yyyy date format in ASP.NET Mvc 如何从窗体主体仅接受ASP.NET WEB API中yyyy-MM-dd格式的日期? - How to accept date only in yyyy-MM-dd format in ASP.NET WEB API from Form body? 在C#中将日期时间格式从mm-dd-yyyy转换为yyyy-mm-dd - datetime format conversion from mm-dd-yyyy to yyyy-mm-dd in c# 如何以 DD/MM/YYYYTHH:mm ASP.NET CORE 5.0 MVC 格式呈现 DateTime - How to render DateTime in format DD/MM/YYYYTHH:mm ASP.NET CORE 5.0 MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM