方法一:在js中格式化时间
JS代码:
function setBirthday(data) {
// /Date(929462400000)/
var Birthday = data.Birthday;
return ChangeDateFormat(Birthday, false);
}
function setBirthTime(data) {
var BirthTime = data.BirthTime;
console.log(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;
}
function setEnterSchoolTime(data) {
// /Date(929462400000)/
var EnterSchoolTime = data.EnterSchoolTime;
return ChangeDateFormat(EnterSchoolTime, true);
}
function ChangeDateFormat(jsondate, isDateTime) {
// 929462400000
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插件
{ templet: setBirthday, title: ‘出生日期’, },
{ templet: setBirthTime, title: ‘出生时间’ },
{ templet: setEnterSchoolTime, title: ‘入学时间’ },
方法二:在服务端格式化时间
创建一个类,并且引用这个类来查询时间
类代码:
private string _StrBirthday;
public string StrBirthday
{
get { return _StrBirthday; }
set
{
//value 代表赋的值
DateTime dt = Convert.ToDateTime(value);
_StrBirthday = dt.ToString(“yyyy-MM-dd”);
}
}//出生日期
private string _StrBirthTime;
public string StrBirthTime
{
get { return _StrBirthTime; }
set
{
DateTime dt = Convert.ToDateTime(value);
_StrBirthTime = dt.ToString(“HH:mm:ss”);
}
}//出生时间
private string _StrEnterSchoolTime;
public string StrEnterSchoolTime
{
get { return _StrEnterSchoolTime; }
set
{
//value 代表赋的值
DateTime dt = Convert.ToDateTime(value);
_StrEnterSchoolTime = dt.ToString(“yyyy-MM-dd HH:mm:ss”);
}
}//入学时间
控制器:
方法一:在js中格式化时间
JS代码:
function setBirthday(data) {
// /Date(929462400000)/
var Birthday = data.Birthday;
return ChangeDateFormat(Birthday, false);
}
function setBirthTime(data) {
var BirthTime = data.BirthTime;
console.log(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;
}
function setEnterSchoolTime(data) {
// /Date(929462400000)/
var EnterSchoolTime = data.EnterSchoolTime;
return ChangeDateFormat(EnterSchoolTime, true);
}
function ChangeDateFormat(jsondate, isDateTime) {
// 929462400000
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插件
{ templet: setBirthday, title: ‘出生日期’, },
{ templet: setBirthTime, title: ‘出生时间’ },
{ templet: setEnterSchoolTime, title: ‘入学时间’ },
方法二:在服务端格式化时间
创建一个类,并且引用这个类来查询时间
类代码:
private string _StrBirthday;
public string StrBirthday
{
get { return _StrBirthday; }
set
{
//value 代表赋的值
DateTime dt = Convert.ToDateTime(value);
_StrBirthday = dt.ToString(“yyyy-MM-dd”);
}
}//出生日期
private string _StrBirthTime;
public string StrBirthTime
{
get { return _StrBirthTime; }
set
{
DateTime dt = Convert.ToDateTime(value);
_StrBirthTime = dt.ToString(“HH:mm:ss”);
}
}//出生时间
private string _StrEnterSchoolTime;
public string StrEnterSchoolTime
{
get { return _StrEnterSchoolTime; }
set
{
//value 代表赋的值
DateTime dt = Convert.ToDateTime(value);
_StrEnterSchoolTime = dt.ToString(“yyyy-MM-dd HH:mm:ss”);
}
}//入学时间
控制器:
效果图: