注意问题:
时间格式返回是秒还毫秒;
//计算创建时间与当前时间的距离
function getTimeSpan(origTime) {
if (origTime < 10000000000)
origTime = origTime * 1000 //秒是10位,如果是小于11位则说明不是毫秒,要加三位变成毫秒
var d = new Date();
var span = d.getTime() - origTime;
var days = Math.floor(span / (24 * 3600 * 1000));
if (days > 0) {
var n = new Date(origTime);
var result = n.getFullYear() + "-" + (n.getMonth() + 1) + "-" + n.getDate();
return result;
}
else {
var leave = Math.floor(span % (24 * 3600 * 1000));
hours = Math.floor(leave / (3600 * 1000));
if (hours > 0) {
return hours + "小时前";
}
else {
var minutes = Math.floor(leave % (3600 * 1000));
minutes = Math.floor(minutes / (60 * 1000));
if (minutes > 0) {
return minutes + "分钟前";
}
else {
return "刚刚";
}
}
}
}
//计算当前时间到结束时间的距离
function timeCountDown(origTime) {
if (origTime < 10000000000)
origTime = origTime * 1000
var d = new Date();
var span = origTime - d.getTime();
if (span<=0) {
return "已结束";
}
var days = Math.floor(span / (24 * 3600 * 1000));
if (days > 0) {
return "还剩" + days + "天结束";
}
else {
var leave = Math.floor(span % (24 * 3600 * 1000));
hours = Math.floor(leave / (3600 * 1000));
if (hours > 0) {
return "还剩" + hours + "小时结束";
}
else {
var minutes = Math.floor(leave % (3600 * 1000));
minutes = Math.floor(minutes / (60 * 1000));
if (minutes > 0) {
return "还剩" + minutes + "分钟结束";
}
else {
return "即将结束";
}
}
}
}
//long型时间转换为格式化的日期时间
function ConvertLongToDate(origTime)
{
if (origTime < 10000000000)
origTime = origTime * 1000
var d = new Date();
var span = d.getTime() - origTime;
var days = Math.floor(span / (24 * 3600 * 1000));
var n = new Date(origTime);
if (days > 0) {
var result = n.getFullYear() + "-" + (n.getMonth() + 1) + "-" + n.getDate();
return result;
}
else {
var result = n.getHours() + ":" + n.getMinutes();
return result;
}
}