繁体   English   中英

如何格式化“dd/MM/yyyy”到日期? c#.NetCore

[英]How to format "dd/MM/yyyy" to date? c# .NetCore

我试图在 Swagger 的示例 json 中显示日期格式“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 BuildingExpenseModeExample: 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"
                }
            }
        };
    }
}

我希望你能帮帮我!

最后,我能够通过将这两行代码添加到我的 Startup 来纠正它:

services.AddControllers().AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
                options.SerializerSettings.DateFormatString = "dd/MM/yyyy";
            });

这样就解决了,因为在我的 api 中已经有一个 DateTimeConverter。 非常感谢大家!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM