时间戳

/**
* 时间转化为 yyyy/m/dd hh:mm:ss
*/
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()

return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

/**
* 时间转化为 yyyy/m/dd
*/
const formatTimes = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()

return [year, month, day].map(formatNumber).join('-')
}

const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}

function formatMsgTime(e) {
function Ztime(value) {
return value >= 10 ? value : '0' + value
}
var dateTime = new Date(e);
var year = dateTime.getFullYear();
var month = dateTime.getMonth() + 1;
var day = dateTime.getDate();
var hour = dateTime.getHours();
var minute = dateTime.getMinutes();
var second = dateTime.getSeconds();
let now = formatTime(new Date());
let now_new = new Date(now).getTime() / 1000;
var milliseconds = 0;
var timeSpanStr;
milliseconds = (now_new - e / 1000) * 1000;
if (milliseconds <= 1000 * 60 * 1) {
timeSpanStr = '刚刚';
} else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
} else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
} else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
} else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == new Date(now).getFullYear()) {
timeSpanStr = month + '-' + day + ' ' + Ztime(hour) + ':' + Ztime(minute);
} else {
timeSpanStr = year + '-' + month + '-' + day + ' ' + Ztime(hour) + ':' + Ztime(minute);;
}
return timeSpanStr;
};
/**
* 获取结束时间与当前时间判断,时间当天不过期,过期为false,不过期为ture
*/
function isExpiredMY(endDate, callBack) {
var cardEndDate = new Date(endDate).getTime() / 1000; //结束时间
var now = new Date(formatTimeMY(new Date())).getTime() / 1000; //当前时间
if (cardEndDate >= now) {
callBack(true);
} else {
callBack(false);
}
};
/**
* 配合上述判断进行时间转换yyyy-mm-dd
*/
function formatTimeMY(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
return [year, month, day].map(formatNumber).join('/')
};

module.exports.formatTimeMY = formatTimeMY;
module.exports.formatMsgTime = formatMsgTime;
module.exports.formatTimes = formatTimes;
module.exports.formatTime = formatMsgTime;
module.exports.isExpiredMY = isExpiredMY;

posted on 2018-11-10 15:14  阿政kris*  阅读(168)  评论(0编辑  收藏  举报