C#时间格式的转换处理
如果时间直接⽤json返回,就会出现下⾯这种情况。数据库中的数据类型到了C#就会发⽣改变。C#中主要有两⼤类型,datetime表⽰年⽉⽇时分秒,timespan表⽰时间差。
在页⾯处理要⽤到⾃定义列表。templet⾃定义列摸版,它可以实现逻辑处理,以及将原始数据转化成其他格式。
处理时间的⽅法,把json类型的时间传进去。将获取到的字符串进⾏处理,⽤正则表达式把时间部分的
数字取出来。然后把取出来的数字转成⽇期类型。西⽅的⽉份是从0开始的,所以要加上1。再进⾏三⽬运算,如果⼩于10就在前⾯加上0,如果⼤于10就正常返回。Date的处理⽅法和⽉份的⼀样,如果⼩于10就在前⾯加上0,如果⼤于10就正常返回。
function ChangeDateFormat(jsondate, isDateTime) {
jsondate = place("/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));
时间正则表达式javavar month = Month() + 1 < 10 ? “0” + (Month() + 1) : Month() + 1;
var currentDate = Date() < 10 ? “0” + Date() : Date();
var str = FullYear() + “-” + month + “-” + currentDate;
if (isDateTime != null && isDateTime != undefined && isDateTime == true) {
var hours = Hours() > 9 ? Hours() : “0” + Hours();
var minutes = Minutes() > 9 ? Minutes() : “0” + Minutes();
var seconds = Seconds() > 9 ? Seconds() : “0” + Seconds();
str = str + " " + hours + “:” + minutes + “:” + seconds;
}
return str;
}
注:上述代码来⾃教学内容。