数据库与js(简单方便)
数据库:
GETDATE()-dtMsgTime as dtMsgTime
要注意的是sql默认时间是 ‘1900-01-01 00:00:00.000’,算出来的时间月份和日期需要减1
GETDATE()-dtMsgTime as dtMsgTime
要注意的是sql默认时间是 ‘1900-01-01 00:00:00.000’,算出来的时间月份和日期需要减1
例如:
select CONVERT(datetime,'2015-08-01 13:12:12')-CONVERT(datetime,'2015-07-31 21:13:45') as dtMsgTime
结果:
1900-01-01 15:58:27.000
月份和天数减1,结果就是15小时58分27秒
JS:
function DateFormat(datDateTime)
{
var intYear = datDateTime.getYear();
var intMonth = datDateTime.getMonth();
var intDate = datDateTime.getDate() - 1;
var intHour = datDateTime.getHours();
var intMinute = datDateTime.getMinutes();
var strResult = "";
if (intYear > 0)
{
strResult = intYear + "年前";
}
if (intMonth > 0 && strResult == "")
{
strResult = intMonth + "个月前";
}
if (intDate > 0 && strResult == "")
{
strResult = intMonth + "天前";
}
if (intHour > 0 && strResult == "")
{
strResult = intHour + "小时前";
}
if (intMinute > 0 && strResult == "")
{
strResult = intMinute + "分钟前";
}
if (intMinute == 0 && strResult == "")
{
strResult = "1分钟内";
}
return strResult;
}
Js调用
var datMsgTime = new Date(dtaSearch.Rows[i][7]);
var strDateTime = DateFormat(datMsgTime);
纯js脚本的时间统计,个人编写,不够效率
var dt_Day = { "Rows": [[1, 31], [2, 28], [3, 31], [4, 30], [5, 31], [6, 30], [7, 31], [8, 31], [9, 30], [10, 31], [11, 30], [12, 31]] }; function TimeInterval(strMsgTime) { var MsgY = FormatDate(strMsgTime, "yyyy"); var MsgM = FormatDate(strMsgTime, "MM"); var MsgD = FormatDate(strMsgTime, "dd"); var MsgH = FormatDate(strMsgTime, "HH"); var MsgMM = FormatDate(strMsgTime, "mm"); var MsgS = FormatDate(strMsgTime, "ss"); var datTime = new Date(); var CurY = FormatDate(datTime, "yyyy"); var CurM = FormatDate(datTime, "MM"); var CurD = FormatDate(datTime, "dd"); var CurH = FormatDate(datTime, "HH"); var CurMM = FormatDate(datTime, "mm"); var CurS = FormatDate(datTime, "ss"); if ((CurY - MsgY) == 0) { if ((CurM - MsgM) == 0) { if ((CurD - MsgD) == 0) { if ((CurH - MsgH) == 0) { if ((CurMM - MsgMM) == 0) { return (CurS - MsgS) + "秒前"; } else { return (CurMM - MsgMM) + "分钟前"; } } else { if((CurMM - MsgMM) < 0) { return 60 - parseInt(MsgMM) + parseInt(CurMM) + "分钟前"; } else { return (CurH - MsgH) + "小时前"; } } } else { return (CurD - MsgD) + "天前"; } } else { if ((CurD - MsgD) < 0) { var MsgMonthSumD; for(var i = 0; i< dt_Day.Rows.length; i++) { if(MsgM == dt_Day.Rows[i][0]) { MsgMonthSumD = dt_Day.Rows[i][1]; break; } } return parseInt(MsgMonthSumD) - parseInt(MsgD) + parseInt(CurD) + "天前"; } else { return parseInt(CurM - MsgM) + "个月" + parseInt(CurD - MsgD) + "天前"; } } } else { var DifferM = parseInt(CurM) + (12 - parseInt(MsgM)); //相差月份 var MsgMonthSumD; for (var i = 0; i < dt_Day.Rows.length; i++) { if (MsgM == dt_Day.Rows[i][0]) { MsgMonthSumD = dt_Day.Rows[i][1]; break; } } if ((CurM - MsgM) < 0)//当前月份小于信息月份 { if ((CurD - MsgD) < 0)// 例如:2014-12-30与2015-01-01 { DifferM = DifferM - 1; if (DifferM > 0) { return DifferM + "个月" + (parseInt(MsgMonthSumD) - parseInt(MsgD) + parseInt(CurD)) + "天前"; } else { return (parseInt(MsgMonthSumD) - parseInt(MsgD) + parseInt(CurD)) + "天前"; } } else if ((CurD - MsgD) == 0) { return DifferM + "个月前"; } else { return DifferM + "个月" + (parseInt(CurD) - parseInt(MsgD)) + "天前"; } } else if ((CurM - MsgM) == 0) { if ((CurD - MsgD) < 0) { DifferM = DifferM - 1; return DifferM + "个月" + (parseInt(MsgMonthSumD) - parseInt(MsgD) + parseInt(CurD)) + "天前"; } else if ((CurD - MsgD) == 0) { return (CurY - MsgY) + "年前"; } else { return (CurY - MsgY) + "年" + (parseInt(CurD) - parseInt(MsgD)) + "天前"; } } else { if ((CurD - MsgD) < 0) { return (CurY - MsgY) + "年" + (parseInt(MsgMonthSumD) - parseInt(MsgD) + parseInt(CurD)) + "天前"; } if ((CurD - MsgD) == 0) { return (CurY - MsgY) + "年" + (parseInt(CurM) - parseInt(MsgM)) + "个月前"; } else { return (CurY - MsgY) + "年" + (parseInt(CurM) - parseInt(MsgM)) + "个月" + (parseInt(CurD) - parseInt(MsgD)) + "天前"; } } } } //时间格式化 function FormatDate(strDate, strFormat) { if (!strDate) return; if (!strFormat) format = "yyyy-MM-dd"; switch (typeof strDate) { case "string": strDate = new Date(strDate.replace(/-/g, "/")); break; case "number": strDate = new Date(strDate); break; } if (!strDate instanceof Date) return; var dict = { "yyyy": strDate.getFullYear(), "M": strDate.getMonth() + 1, "d": strDate.getDate(), "H": strDate.getHours(), "m": strDate.getMinutes(), "s": strDate.getSeconds(), "MM": ("" + (strDate.getMonth() + 101)).substr(1), "dd": ("" + (strDate.getDate() + 100)).substr(1), "HH": ("" + (strDate.getHours() + 100)).substr(1), "mm": ("" + (strDate.getMinutes() + 100)).substr(1), "ss": ("" + (strDate.getSeconds() + 100)).substr(1) }; return strFormat.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function () { return dict[arguments[0]]; }); }