.net core 中 DateTime 序列化为json后带字母T问题

 当实体类中的类型为DateTime类型时(如下)

//实体
class Model{
    //...
    //创建时间
    public DateTime CreateTime{get;set;}
    //...
}

再进行json序列化后,前端收到的结果为xxxx-xx-xxT xx:xx:xx 如:2018-11-03T15:20:20

原因是core版本中的Newtonsoft.Json默认使用的是ISO格式

解决办法:

第一种:后端指定序列化为时间戳,前端自己根据需要转换格式

1、打开Startup.cs文件,在ConfigureServices方法中添加如下代码

public void ConfigureServices(IServiceCollection services){
    //....其他代码
    //需要添加的代码开始
    services.AddMvc().AddJsonOptions(option=>{
        //配置大小写问题,默认是首字母小写,该配置根据项目需要更改,我是怕忘了,所以写在这里
        //option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
        //配置序列化时时间格式为时间戳
        option.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
    })
    //需要添加的代码开始
}

2、前端格式化时间格式代码

(function () {
    //日期扩展
    Date.prototype.format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份   
            "d+": this.getDate(), //日   
            "H+": this.getHours(), //小时   
            "m+": this.getMinutes(), //分   
            "s+": this.getSeconds(), //秒   
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
            "S": this.getMilliseconds() //毫秒   
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o) {
            if (o.hasOwnProperty(k)) {
                if (new RegExp("(" + k + ")").test(fmt))
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return fmt;
    };
    //字符串扩展
    String.prototype.getDate = function () {
        console.log(this)
        var res = /\d{13}/.exec(this);
        var date = new Date(parseInt(res));
        return date.format("yyyy-MM-dd HH:mm:ss");
    }
})();

 3、前端接收到后台的时间戳字符串后调用

let timeString="1541243166"//该值应该为后台传过来的时间戳,为演示,这里指定固定值
console.log(timeString.getDate();)//输出结果为:2018-11-03 19:06:06

第二种:后端更改为指定字符串格式如:yyyy-MM-dd HH-mm-ss

1、打开Startup.cs文件,在ConfigureServices方法中添加如下代码

public void ConfigureServices(IServiceCollection services){
    //....其他代码
    //需要添加的代码开始
    services.AddMvc().AddJsonOptions(option=>{
        //配置大小写问题,默认是首字母小写
        option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
        //配置序列化时时间格式为yyyy-MM-dd HH:mm:ss
        option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
    })
    //需要添加的代码开始
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值