很多时候为了提高查询的效率,我们的数据表中的字段,需要将存放的日期格式的数据,存放为“long” 或者是“varchar” 类型
这时候就需要我们从页面取数据的时候进行转换。
<script type="text/javascript">
Date.prototype.format = function (format) {
var o = {"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
function getFormatDateByLong(l, pattern) {
return getFormatDate(new Date(l), pattern);
}
function getFormatDate(date, pattern) {
if (date == undefined) {
date = new Date();
}
if (pattern == undefined) {
pattern = "yyyy-MM-dd hh:mm:ss";
}
return date.format(pattern);
}
//定义日期格式
var pattern = "yyyy-MM-dd hh:mm:ss";
//下面的param为你得到的数据库表中的字段的值,如:(1279849429)
记得如果是10为的话,一定要乘以1000,如果数据库中的数据是13位的话,就不需要了
getFormatDateByLong(param*1000,pattern);
</script>