123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- import dayjs from 'dayjs';
- export function formatSeconds(seconds, type) {
- let hours = Math.floor(seconds / 3600);
- let minutes = Math.floor((seconds - (hours * 3600)) / 60);
- let secs = seconds - (hours * 3600 + minutes * 60);
- hours = hours == 0 ? '' : hours.toString().padStart(2, '0');
- minutes = hours == 0 && minutes == 0 ? '' : minutes.toString().padStart(2, '0');
- secs = hours == 0 && minutes == 0&&secs == 0 ? '' : secs.toString().padStart(2, '0');
- // 汉字1,:隔开0
- if(type == 1) {
- return `${hours&&hours+'时'}${minutes&&minutes+'分'}${secs&&secs+'秒'}`
- } else {
- return `${hours&&hours+':'}${minutes&&minutes+':'}${secs}`
- }
- };
- //新增
- var friendlyDate=function(timestamp) {
- var formats = {
- 'year': '%n% 年前',
- 'month': '%n% 月前',
- 'day': '%n% 天前',
- 'hour': '%n% 小时前',
- 'minute': '%n% 分钟前',
- 'second': '%n% 秒前',
- };
- var now = Date.now();
- var seconds = Math.floor((now - timestamp) / 1000);
- var minutes = Math.floor(seconds / 60);
- var hours = Math.floor(minutes / 60);
- var days = Math.floor(hours / 24);
- var months = Math.floor(days / 30);
- var years = Math.floor(months / 12);
-
- var diffType = '';
- var 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 {Object} 时间字符串
- // */
-
- export function formatDate(dateStr) {
- let formatStr = "";
- let formatDate=null;
- if(dateStr==null){
- return formatStr;
- }
- let date = dayjs(dateStr,'YYYY-MM-DD HH:mm:ss');
- let today = dayjs();
- if (date.year() != today.year()) {
- formatDate = date.format('YYYY-MM-DD HH:mm');
- }
- else if (date.month() === today.month()) {
- switch (date.date() - today.date()) {
- case 0:
- formatDate = date.format('今天 HH:mm');
- break;
- case -1:
- formatDate = date.format('昨天 HH:mm');
- break;
- case 1:
- formatDate = date.format('明天 HH:mm');
- break;
- default:
- formatDate = date.format('MM-DD HH:mm');
- break;
- }
- }
- else if (date.month() - today.month() === 0 && date.date() === 1) {
- formatDate = date.format('明天 HH:mm');
- }
- else {
- formatDate = date.format('MM-DD HH:mm');
- }
- return formatDate;
- }
-
-
- // /**
- // * 格式化时间
- // * @param {Object} 时间字符串
- // */
- var formatHour=function(dateStr) {
- let date = dayjs(dateStr,'YYYY-MM-DD HH:mm:ss');
- let formatHour = date.format('HH:mm');
- return formatHour;
- }
- // /**
- // * 计算时间差值
- // * @param {startTime} 开始时间
- // * @param {endTime} 结束时间
- // * @param {type} 类型
- // */
- var calculateDiffTime=function(startTime, endTime,type=4) {
- // let timeStarts = startTime.getTime(); //开始时间,转换成时间戳
- // let timeEnds = endTime.getTime(); //结束时间,转换成时间戳
-
- let timer = endTime - startTime //将时间戳进行相减
-
- let year = parseInt((timer / 1000 / 60 / 60 / 24 / 30 / 12));
- let month = parseInt((timer / 1000 / 60 / 60 / 24 / 30));
- let day = parseInt((timer / 1000 / 60 / 60 / 24));
- let hour = parseInt((timer / 1000 / 60 / 60 % 24));
- let minute = parseInt((timer / 1000 / 60 % 60));
- let second = parseInt((timer / 1000 % 60));
- if(hour>0){
- return (hour>10?hour:'0'+hour) + ':' + (minute>10?minute:'0'+minute) + ':' + (second>10?second:'0'+second);
- }else if(minute>0){
- return (minute>=10?minute:'0'+minute) + ':' + (second>=10?second:'0'+second);
- }else if(second>0){
- return '00:'+(second>=10?second:'0'+second);
- }
-
- // if (type === 1) { // 返回相差年数
- // return year + '年'
- // } else if (type === 2) { // 返回相差年数月数
- // return year + '年' + month + '月'
- // } else if (type === 3) { // 返回相差年数月数天数
- // return year + '年' + month + '月' + day + '日'
- // } else { // 返回相差年数月数天数时分秒
- // return year + '年' + month + '月' + day + '日' + hour + '时' + minute + '分' + second + '秒'
- // }
-
- }
-
|