去掉时间格式(yyyy:MM:ddThh:mm:ss)中间的“T”的前后台两种方法
程序员文章站
2022-06-02 11:42:09
...
问题展示如下,取出的时间中间含有T:
[
{
"imgUrl": "http://localhost:44375/imgs/20201218/123.PNG",
"id": 1,
"loginId": 1,
"loginName": "liubang",
"name": "刘邦",
"phone": "13713141222",
"publishContent": "我刘邦贼牛皮",
"createTime": "2020-04-09T00:00:00" //时间格式中含有T
},
{.....}]
后台解决方法
在startup.cs文件中,在如下位置添加格式转换代码
.....
public void ConfigureServices(IServiceCollection services)
{
......
//JSON格式化
services.AddMvc().AddNewtonsoftJson(options =>
{
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
});
//参数.序列化设置.数据格式化字符串=“规则”
//AddNewtonsoftJson需要项目中引用一个包
}
在项目中NuGet包管理器中安装一个包:Newtonsoft
安装后位置如下:
前台解决方法
在JS中,对获取的时间进行正则表达式修饰即可 .replace(/T/g, ' ').replace(/.[\d]{3}Z/, ' ')
缺点:每次用到都要进行修饰,太过于繁琐,不推荐
示例一:
getData() {
wx.request({
url: 'http://[ip]/api/PublishViews',
success(res) {
console.log(res.data)
var temp = res.data
for (var i = 0; i < temp.length; i++)
{
temp[i].createTime=temp[i].createTime.replace(/T/g, ' ').replace(/.[\d]{3}Z/, ' ');
}
that.setData({
posts: temp,
})
wx.hideLoading({})
wx.stopPullDownRefresh({})
}
})
},
示例二:
onLoad: function (options) {
that = this
wx.request({
url: 'http://[ip]/api/PublishViews/'+options.id,
success(res) {
that.setData({
....
dateTime:res.data.createTime.replace(/T/g, ' ').replace(/.[\d]{3}Z/, ' '),
....
})
}
})
},
上一篇: 回顾JQuery知识
下一篇: 新手thinkphp应该如何学