js 时间戳显示

$.UnixToDate(data1.data.expirationDate*1000)

$.UnixToDate($(this).attr("expirationDate")*1000).replace(/\//g,'-')   ,正则表达式,把"/"全部替换成"-"符号

jQuery.extend({

/**
* 毫秒转成年月日时分秒,true转成年月日时分秒,false转成年月日
* @param <string> 2014-01-01 20:20:20 日期格式
* @return <int> unix时间戳(秒)
*/
UnixToDate: function (unixTime, isFull, timeZone) {
if (typeof (timeZone) == 'number') {
unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60;
}
var time = new Date(unixTime);
var ymdhis = "";
ymdhis += time.getFullYear() + "/";
ymdhis +=
(time.getMonth() + 1) < 10 ? "0" + (time.getMonth() + 1) + "/"
: (time.getMonth() + 1) + "/";
ymdhis += time.getDate() < 10 ? "0" + time.getDate() : time.getDate();
if (isFull === true) {
ymdhis +=
time.getHours() < 10 ? " 0" + time.getHours() + ":" : " "
+ time.getHours()
+ ":";
ymdhis +=
time.getMinutes() < 10 ? "0" + time.getMinutes() + ":" : ""
+ time.getMinutes()
+ ":";
ymdhis +=
time.getSeconds() < 10 ? "0" + time.getSeconds() : time.getSeconds();
}
return ymdhis;
},
/**
* 日期 转换为 Unix时间戳
* @param <string> 2014-01-01 20:20:20 日期格式
* @return <int> unix时间戳(秒)
*/
DateToUnix: function(string) {
var f = string.split(' ', 2);
var d = (f[0] ? f[0] : '').split('-', 3);
var t = (f[1] ? f[1] : '').split(':', 3);
return (new Date(
parseInt(d[0], 10) || null,
(parseInt(d[1], 10) || 1) - 1,
parseInt(d[2], 10) || null,
parseInt(t[0], 10) || null,
parseInt(t[1], 10) || null,
parseInt(t[2], 10) || null
)).getTime() / 1000;
},
/**
* 毫秒转成年月日时分秒
* @param <string> 2014-01-01 20:20:20 日期格式
* @return <int> unix时间戳(秒)
*/
getDate: function (tm) {
var now = new Date(tm);
var year = now.getFullYear();
var month = now.getMonth() + 1;
var date = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
return year + "-" + month + "-" + date + " " + hour + ":" + minute;
},
getDateshort:function(tm)
{
var now = new Date(tm);
var year = now.getFullYear();
var month = now.getMonth() + 1;
var date = now.getDate();
return year + "-" + month + "-" + date;
},
/**
* 将毫秒转成时分秒
* @param <string>
* @return <int>
*/
formatSeconds(value) {
var theTime = parseInt(value);// 秒
var theTime1 = 0;// 分
var theTime2 = 0;// 小时
// alert(theTime);
if(theTime > 60) {
theTime1 = parseInt(theTime/60);
theTime = parseInt(theTime%60);
// alert(theTime1+"-"+theTime);
if(theTime1 > 60) {
theTime2 = parseInt(theTime1/60);
theTime1 = parseInt(theTime1%60);
}
}
var result = ""+parseInt(theTime)+"秒";
if(theTime1 > 0) {
result = ""+parseInt(theTime1)+"分"+result;
}
if(theTime2 > 0) {
result = ""+parseInt(theTime2)+"小时"+result;
}
return result;
}
});

 

 

定义和用法

toFixed() 方法可把 Number 四舍五入为指定小数位数的数字。

语法

NumberObject.toFixed(num)

 

posted @ 2017-08-02 13:49  szchenrong  阅读(266)  评论(0)    收藏  举报