时间转换(一)

敲可耐的我 2019-07-17 09:04:08
时间转换(一)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
本次任务完成时间:2019年07月18日
开发工具与关键技术:Visual Studio 2015 && 时间转换(一)
展示效果:时间转换
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我们平时写数据表格只要给的字段名和数据库的字段名一样,数据就会出来,但是有一种情况,是不会出来,这种情况就是日期和时间,时间类型被json序列化成一堆看不懂的英文和数字,

我们就需要进行时间转换,本章的内容会告诉大家两种时间转换;第一个是在视图进行时间转换,SQL里面的date类型和datetime类型映射到C#里面是DateTime类型,time类型和timestamp类型映射过来是TimeSpan类型,这个是它们的类型关系,在js中格式化时间;在视图写了个时间转换的方法;

function ChangeDateFormat(jsondate, isDateTime) {
jsondate = jsondate.replace("/Date(", "").replace(")/", "");
if (jsondate.indexOf("+") > 0) {
jsondate = jsondate.substring(0, jsondate.indexOf("+"));
}
else if (jsondate.indexOf("-") > 0) {
jsondate = jsondate.substring(0, jsondate.indexOf("-"));
}
var date = new Date(parseInt(jsondate, 10));
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
var str = date.getFullYear() + "-" + month + "-" + currentDate;
if (isDateTime != null && isDateTime != undefined && isDateTime == true) {
var hours = date.getHours() > 9 ? date.getHours() : "0" + date.getHours();
var minutes = date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes();
var seconds = date.getSeconds() > 9 ? date.getSeconds() : "0" + date.getSeconds();
str = str + " " + hours + ":" + minutes + ":" + seconds;
}
return str;
}
在layui表格渲染中的时间字段给他设为自定义

接着直接调用上面封装的方法,
function setBirthday(data) {
var Birthday = data.Birthday;
return ChangeDateFormat(Birthday, false);
}
在后面加了False是因为这个字段名只需要日期,若日期和时间都需要改成true就可以了
时间的转换代码量要多一点点,和上面封装的方法一样,改一下就可以了。
function setBirthTime(data) {
var BirthTime = data.BirthTime;
var hours = BirthTime.Hours > 9 ? BirthTime.Hours : "0" + BirthTime.Hours;
var minutes = BirthTime.Minutes > 9 ? BirthTime.Minutes : "0" + BirthTime.Minutes;
var seconds = BirthTime.Seconds > 9 ? BirthTime.Seconds : "0" + BirthTime.Seconds;
return hours + ":" + minutes + ":" + seconds;
}
时间转换后看一下效果

内容未完,请看下一章……
...全文
51 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

488

社区成员

发帖
与我相关
我的任务
社区描述
硬件使用 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧