首页 > 其他分享 >2-返回数据的时间格式化

2-返回数据的时间格式化

时间:2023-01-03 23:13:32浏览次数:32  
标签:返回 AddControllers 格式化 builder Services 数据 options

2-返回数据的时间格式化

在我们返回数据的时候,我们返回的Json时间是很长的一串,很明显这对前端并不友好

所以我们要对这个时间进行格式化处理

{
    "date": "2022-01-13T23:29:26.8882492+08:00",
    "temperatureC": 48,:
    "temperatureF": 118,
    "summary": "Sweltering"
}

处理的方式很简单

我们先安装一个扩展包

nuget安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson

只需要在Program.cs 文件下添加几行代码
找到builder.Services.AddControllers()

builder.Services.AddControllers()
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
    });

就轻松解决了

转载自https://bxshare.top/index.php/archives/11/

标签:返回,AddControllers,格式化,builder,Services,数据,options
From: https://www.cnblogs.com/ABSchurke/p/17023642.html

相关文章