我试图在一个示例json中以Swagger格式显示日期格式“dd/MM/yyyy”。然而,当我想展示它时,它向我展示如下:
{
"contractCode": 0,
"registers": 0,
"totalDue1": 0,
"totalDue2": 0,
"buildingExpenses": [
{
"ownersAssociationCode": 0,
"functionalUnitCode": 0,
"period": "hola",
"ownerName": "string",
"location": "string",
"ownerEmail": "string",
"dueDate1": "2021-12-20T00:00:00",
"amount1": 0,
"amount2": 0,
"electronicPaymentCode": "string",
"barcode": "string"
}
]
}
我试图用parse和parseExact格式化它,但两者都不起作用。我留下我的代码:public class buildingexpensemode示例:IExamplesProvider
{
public object GetExamples()
{
var dueDate1 = DateTime.Now.ToString("dd/MM/yyyy");
return new BuildingExpenseResumeInputDto
{
ContractCode = 0,
Registers = 0,
TotalDue1 = 0,
TotalDue2 = 0,
BuildingExpenses = new List<BuildingExpenseDetailInputDto>
{
new BuildingExpenseDetailInputDto
{
OwnersAssociationCode = 0,
FunctionalUnitCode = 0,
Period = "hola",
OwnerName = "string",
Location = "string",
OwnerEmail = "string",
DueDate1 = DateTime.ParseExact(dueDate1, "dd/MM/yyyy", CultureInfo.InvariantCulture),
//DueDate1 = DateTime.ParseExact(DateTime.Today.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture),
Amount1 = 0,
Amount2 = 0,
ElectronicPaymentCode = "string",
Barcode = "string"
}
}
};
}
}
我希望你能帮助我!
最后,我通过在我的启动中添加这两行代码来纠正它:
services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
options.SerializerSettings.DateFormatString = "dd/MM/yyyy";
});
解决了这个问题,因为在我的api中已经有了DateTimeConverter。非常感谢大家!