jquery 毫秒转换成日期_jquery 时间戳转换为日期

1.转换为年月日

new Date(data.createDate).toLocaleDateString()//将json中的时间戳转换为年月日

2.精确到秒

function getMyDate(str){

var oDate = new Date(str),

oYear = oDate.getFullYear(),

oMonth = oDate.getMonth()+1,

oDay = oDate.getDate(),

oHour = oDate.getHours(),

oMin = oDate.getMinutes(),

oSen = oDate.getSeconds(),

oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间

return oTime;

};

//补0操作

function getzf(num){

if(parseInt(num) < 10){

num = '0'+num;

}

return num;

}

3.精确到毫秒

function getMyDate(str){

var oDate = new Date(str),

oYear = oDate.getFullYear(),//年

oMonth = oDate.getMonth()+1,//月

oDay = oDate.getDate(),//日

oHour = oDate.getHours(),//时

oMin = oDate.getMinutes(),//分

oSen = oDate.getSeconds(),//秒

oFf=oDate.getMilliseconds()//毫秒

oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen)+':'+getzf(oFf);//最后拼接时间

return oTime;

};

//补0操作

function getzf(num){

if(parseInt(num) < 10){

num = '0'+num;

}

return num;

}

4.重写tolocaleString方法

Date.prototype.toLocaleString = function() {

return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + "日 " + this.getHours() + "点" + this.getMinutes() + "分" + this.getSeconds() + "秒";

};

new Date(此处放时间戳).toLocaleString();

5.时间格式转换各种方法

var mydate = new Date();

mydate.getYear(); //获取当前年份(2位)

mydate.getFullYear(); //获取完整的年份(4位,1970-????)

mydate.getMonth(); //获取当前月份(0-11,0代表1月)

mydate.getDate(); //获取当前日(1-31)

mydate.getDay(); //获取当前星期X(0-6,0代表星期天)

mydate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)

mydate.getHours(); //获取当前小时数(0-23)

mydate.getMinutes(); //获取当前分钟数(0-59)

mydate.getSeconds(); //获取当前秒数(0-59)

mydate.getMilliseconds(); //获取当前毫秒数(0-999)

mydate.toLocaleDateString(); //获取当前日期

var mytime=mydate.toLocaleTimeString(); //获取当前时间

mydate.toLocaleString( ); //获取日期与时间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值