123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- /*若小于10,前面加0*/
- function isZero(m) {
- return m < 10 ? '0' + m : m
- }
- function friendlyDate(timestamp) {
- let formats = {
- 'year': '%n% 年前',
- 'month': '%n% 月前',
- 'day': '%n% 天前',
- 'hour': '%n% 小时前',
- 'minute': '%n% 分钟前',
- 'second': '%n% 秒前',
- };
- let now = Date.now();
- let seconds = Math.floor((now - timestamp) / 1000);
- let minutes = Math.floor(seconds / 60);
- let hours = Math.floor(minutes / 60);
- let days = Math.floor(hours / 24);
- let months = Math.floor(days / 30);
- let years = Math.floor(months / 12);
- let diffType = '';
- let diffValue = 0;
- if (years > 0) {
- diffType = 'year';
- diffValue = years;
- } else {
- if (months > 0) {
- diffType = 'month';
- diffValue = months;
- } else {
- if (days > 0) {
- diffType = 'day';
- diffValue = days;
- } else {
- if (hours > 0) {
- diffType = 'hour';
- diffValue = hours;
- } else {
- if (minutes > 0) {
- diffType = 'minute';
- diffValue = minutes;
- } else {
- diffType = 'second';
- diffValue = seconds === 0 ? (seconds = 1) : seconds;
- }
- }
- }
- }
- }
- return formats[diffType].replace('%n%', diffValue);
- }
- /*
- * 时间格式转换
- * @param timestamp 时间 String || Date (123456 && 1978-10-25 10:25:20 && data对象)
- * @param format 输出格式 String (slash: 1978/10/25 10:25:20 horizontal: 1978-10-25 10:25:20 minute:10:25 second:10:25)
- */
- function formatDate(timestamp, format = 'slash') {
- //时间戳是整数,否则要parseInt转换
- let time = '';
- console.log(timestamp.constructor);
- if (timestamp.constructor === Date) {
- time = timestamp;
- } else {
- time = (parseFloat(timestamp) + 946684800).toString();
- if (time.indexOf('-') != -1 || time.indexOf('/') != -1 || time.indexOf(':') != -1) {
- //将时间字符串转成时间戳
- time = time.replace(/-/g, '/') //为了兼容IOS,需先将字符串转换为'2018/9/11 9:11:23'
- time = new Date(time);
- } else {
- if (time.length == 10) {
- time = new Date(parseInt(time) * 1000);
- } else if (time.length == 13) {
- time = new Date(parseInt(time));
- }
- }
- }
- let y = time.getFullYear();
- let m = time.getMonth() + 1;
- let d = time.getDate();
- let h = time.getHours();
- let mm = time.getMinutes();
- let s = time.getSeconds();
- let now = new Date();
- let seconds = Math.floor((now - time));
- let minutes = Math.floor(seconds / 60);
- let hours = Math.floor(minutes / 60);
- let days = Math.floor(hours / 24);
- let months = Math.floor(days / 30);
- let years = Math.floor(months / 12);
- let NowY = (new Date()).getFullYear();
- let dayTime = 24 * 60 * 60;
- //当前时间
- let td = new Date();
- td = new Date(td.getFullYear(), td.getMonth(), td.getDate());
- //传递时间
- let od = new Date(time);
- od = new Date(od.getFullYear(), od.getMonth(), od.getDate());
- //当前时间与传递时间的差值
- let xc = (od - td) / 1000 / 60 / 60 / 24;
- //return y+'/'+isZero(m)+'/'+isZero(d)+' '+isZero(h)+':'+isZero(mm)+':'+isZero(s);
- switch (format) {
- case "horizontal":
- return y + '-' + isZero(m) + '-' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm) + ':' + isZero(s);
- break;
- case "day":
- return y + '-' + isZero(m) + '-' + isZero(d);
- break;
- case "minute":
- let result = '';
- if (NowY === y) {
- if (xc < -1) {
- result = isZero(m) + '-' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm)
- } else if (xc < 0) {
- result = '昨天' + ' ' + isZero(h) + ':' + isZero(mm)
- } else if (xc == 0) {
- result = isZero(h) + ':' + isZero(mm)
- };
- } else {
- result = y + '-' + isZero(m) + '-' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm)
- }
- return result;
- break;
- case "second":
- let result2 = '';
- if (NowY === y) {
- if (xc < -1) {
- result2 = isZero(m) + '-' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm) + ':' + isZero(s)
- } else if (xc < 0) {
- result2 = '昨天' + ' ' + isZero(h) + ':' + isZero(mm) + ':' + isZero(s)
- } else if (xc == 0) {
- result2 = isZero(h) + ':' + isZero(mm) + ':' + isZero(s)
- };
- } else {
- result2 = y + '-' + isZero(m) + '-' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm) + ':' + isZero(s)
- }
- return result2;
- break;
- case "spot":
- let result3 = '';
- result3 = y + '.' + isZero(m) + '.' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm) + ':' + isZero(s)
- return result3;
- break;
- case "date":
- let result4 = '';
- result4 = y + '.' + isZero(m) + '.' + isZero(d);
- return result4;
- break;
- case "time":
- let result5 = '';
- result5 = isZero(h) + ':' + isZero(mm);
- return result5;
- break;
- case "dateTime":
- let result6 = '';
- result6 = y + '-' + isZero(m) + '-' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm);
- return result6;
- break;
- case "bvcTime":
- return isZero(h) + ':' + isZero(mm) + ':' + isZero(s) + ' ' + isZero(d) + '/' + isZero(m) + '/' + y;
- break;
- default:
- return y + '/' + isZero(m) + '/' + isZero(d) + ' ' + isZero(h) + ':' + isZero(mm) + ':' + isZero(s);
- }
- }
- export {
- friendlyDate,
- formatDate
- }
|