用做公共方法放在util.js内
main.js内
import util from '@/util/util'
Vue.prototype.$util = util
获取
一、根据日期获取周几
调用方法:
let myDate = new Date(); //获取今天日期
let e = this.$util.getweekdayDate(myDate,0) // 传0就是今天 1就是明天 、-1就是昨天以此类推
组件内代码:
getweekdayDate: function(date,day) {
let days = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
let today = new Date();
today.setDate(today.getDate() + day);
let weekday = today.getDay();
let dd = days[weekday];
return dd;
},
二、年月日
调用方法:
let myDate = new Date(); //获取今天日期
this.$util.getNextDate(myDate,0) // 传0就是今天 1就是明天 、-1就是昨天以此类推
组件内代码:
getNextDate: function(date,day) {
const dd = new Date(date);
dd.setDate(dd.getDate() + day);
const y = dd.getFullYear();
const m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
const d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
return y + "-" + m + "-" + d;
},
三、年月日时分秒
调用方法:
let myDate = new Date(); //获取今天日期
this.$util.getNextDate(myDate,0) // 传0就是今天 1就是明天 、-1就是昨天以此类推
组件内代码:
getNextDate(date,day) {
var dd = new Date(date);
dd.setDate(dd.getDate() + day);
var y = dd.getFullYear();
var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
var h = dd.getHours() < 10 ? "0" + dd.getHours() : dd.getHours();
var t = dd.getMinutes() < 10 ? "0" + dd.getMinutes() : dd.getMinutes();
var s = dd.getSeconds() < 10 ? "0" + dd.getSeconds() : dd.getSeconds();
let time = y + "-" + m + "-" + d + ' ' + h + ':'+ t + ':' + s
return time;
},
四、年月日 星期几 时分秒
var date = new Date();
this.year = date.getFullYear();
this.month = date.getMonth() + 1;
this.date = date.getDate();
this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
转换
一、日期格式转换--时间字符串转化成视觉格式
// 使用示例:this.$util.formatDate('/Date(1435646188067+0800)/','yyyy-MM-dd hh:mm:ss')
// 使用示例:this.$util.formatDate('2018-12-12T12:12:12','yyyy-MM-dd hh:mm:ss')
// 使用示例:this.$util.formatDate('2018-12-12 12:12:12','yyyy/MM/dd hh:mm:ss')
// 使用示例:this.$util.formatDate(new Date(),'yyyy/MM/dd hh:mm:ss')
// 作为方法调用:this.$util.formatDate(时间,[格式])
// 作为filter调用:{{时间 | formatDate([格式])}}
formatDate: function(v, format) {
if (!v) return ''
var d = v
if (typeof v === 'string') {
if (v.indexOf('/Date(') > -1) {
d = new Date(parseInt(v.replace('/Date(', '').replace(')/', ''), 10))
} else {
d = new Date(Date.parse(v.replace(/-/g, '/').replace('T', ' ').split('.')[0]))
} // .split(".")[0] 用来处理出现毫秒的情况,截取掉.xxx,否则会出错
}
var o = {
'M+': d.getMonth() + 1,
'd+': d.getDate(),
'h+': d.getHours(),
'm+': d.getMinutes(),
's+': d.getSeconds(),
'q+': Math.floor((d.getMonth() + 3) / 3),
'S': d.getMilliseconds()
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length))
}
for (var k in o) {
if (new RegExp('(' + k + ')').test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
}
}
return format
},
二、时间格式数据转化成 刚刚 n分钟前 n小时前 今天hh:mm 昨天 hh:mm MM/dd hh:mm yyyy/MM/dd hh:mm
// 时间格式可以是 时间对象;yyyy/MM/dd hh:mm:ss;yyyy-MM-dd hh:mm:ss;
'/Date(1435646188067+0800)/';'2018-12-12T12:12:12';等任意合法的时间格式
formatTimestamp: function(v) {
const timeStr = this.formatDate(v, 'yyyy/MM/dd hh:mm:ss')
const timestamp = parseInt(new Date(timeStr).getTime() / 1000);
function zeroize(num) {
return (String(num).length === 1 ? '0' : '') + num;
}
var curTimestamp = parseInt(new Date().getTime() / 1000); // 当前时间戳
var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数
var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象
var Y = tmDate.getFullYear();
var m = tmDate.getMonth() + 1;
var d = tmDate.getDate();
var H = tmDate.getHours();
var i = tmDate.getMinutes();
// var s = tmDate.getSeconds();
if (timestampDiff < 60) { // 一分钟以内
return '刚刚';
} else if (timestampDiff < 3600) { // 一小时前之内
return Math.floor(timestampDiff / 60) + '分钟前';
} else if (curDate.getFullYear() === Y && curDate.getMonth() + 1 === m && curDate.getDate() === d) {
return '今天 ' + zeroize(H) + ':' + zeroize(i);
} else {
var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
if (newDate.getFullYear() === Y && newDate.getMonth() + 1 === m && newDate.getDate() === d) {
return '昨天 ' + zeroize(H) + ':' + zeroize(i);
} else if (curDate.getFullYear() === Y) {
return zeroize(m) + '/' + zeroize(d) + ' ' + zeroize(H) + ':' + zeroize(i);
} else {
// return Y + '/' + zeroize(m) + '/' + zeroize(d) + ' ' + zeroize(H) + ':' + zeroize(i);
return Y + '/' + zeroize(m) + '/' + zeroize(d);
}
}
},