js将10位long类型的数据转换为标准的date日期类型格式数据

11 篇文章 0 订阅
5 篇文章 0 订阅

很多时候为了提高查询的效率,我们的数据表中的字段,需要将存放的日期格式的数据,存放为“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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌谚轩

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值