1:生成普通时间格式数据.
//生成时间格式
function getTime() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
var Hours = date.getHours();
var Minutes = date.getMinutes();
var Seconds = date.getSeconds();
//描述一下,在生成时间格式时,如果小时数小于10,例:2020-4-18 9:58:8 是这样的格式对象。在数据库排序查询时会有混乱,
// A:2020-4-18 9:58:8 B:2020-04-18 08:58:08 查询出来,A会在B的前面。
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (Hours >= 0 && Hours <= 9) {
Hours = "0" + Hours;
}
if (Minutes >= 0 && Minutes <= 9) {
Minutes = "0" + Minutes;
}
if (Seconds >= 0 && Seconds <= 9) {
Seconds = "0" + Seconds;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + Hours + seperator2 + Minutes + seperator2 + Seconds;
return currentdate;
}
console.log(getTime())
//得到2020-04-18 10:00:58
2.获取减去15天的时间格式数据.
/**
* 获取减去多少天的时间。
* @param {*} fmt 格式化参数
* @param {*} numbers 减去多少天
*/
function format(fmt, numbers) {
var date = new Date()
date.setDate(numbers)
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(
RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
}
console.log(format("yyyy-MM-dd hh:mm:ss", new Date().getDate() - 15))
//现在我的时间是2020-04-18 10:04:55 得到 2020-04-03 10:04:55
3.生成本月第一天,最后一天,上月第一天,最后一天时间格式数据。
/**
* 生成 本月,上月时间
* @param {*} dateSize
*/
function TimeFormat(str) {
var date = new Date();
if (str === "lastMonthMin") {
if (date.getMonth() + 1 === 1) {
var Year = new Date().getFullYear() - 1;
return Year + "-" + "12" + "-01 00:00:00"
} else {
var Year = new Date().getFullYear();
var Month = (new Date().getMonth() + 1) >= 10 ? (new Date().getMonth() + 1) : ('0' + ((new Date().getMonth() + 1) - 1))
return Year + "-" + Month + "-01 00:00:00"
}
}
if (str === "lastMonthMax") {
if (date.getMonth() + 1 === 1) {
var Year = new Date().getFullYear() - 1;
//获取本月天数(获取后一个月的0日即前一月的最后一日)
var totalDay = (new Date(date.getFullYear(), parseInt(date.getMonth() + 1), 0)).getDate();
return Year + "-" + "12" + "-" + totalDay + " 23:59:59"
} else {
var Year = new Date().getFullYear();
var Month = (new Date().getMonth() + 1) >= 10 ? (new Date().getMonth() + 1) : ('0' + ((new Date().getMonth() + 1) - 1))
//获取本月天数(获取后一个月的0日即前一月的最后一日)
var totalDay = (new Date(date.getFullYear(), parseInt(date.getMonth() + 1), 0)).getDate();
return Year + "-" + Month + "-" + totalDay + " 23:59:59"
}
}
if (str === "thisMonthMin") {
var Year = new Date().getFullYear();
var Month = (new Date().getMonth() + 1) >= 10 ? (new Date().getMonth() + 1) : ('0' + (new Date().getMonth() + 1))
return Year + "-" + Month + "-01 00:00:00"
}
if (str === "thisMonthMax") {
var Year = new Date().getFullYear();
var Month = (new Date().getMonth() + 1) >= 10 ? (new Date().getMonth() + 1) : ('0' + (new Date().getMonth() + 1));
var totalDay = (new Date(date.getFullYear(), parseInt(date.getMonth() + 1), 0)).getDate();
return Year + "-" + Month + "-" + totalDay + " 23:59:59"
}
}
console.log(TimeFormat("lastMonthMin"))
console.log(TimeFormat("lastMonthMax"))
console.log(TimeFormat("thisMonthMin"))
console.log(TimeFormat("thisMonthMax"))
//2020-03-01 00:00:00
//2020-03-30 23:59:59
//2020-04-01 00:00:00
//2020-04-30 23:59:59
4.两个时间相减
//安装moment库
cnpm install moment --save
//举个栗子,拿退换货来说。一般收到物流7天可退,14天可换。
computed:{
calculateRefundTime(){
if(this.Trace){
var now = moment().format('YYYY-MM-DD hh:mm:ss');
var s1 = new Date(now.replace(/-/g, "/"));
var s2 = new Date(this.Trace.Traces[0].AcceptTime.replace(/-/g, "/"));
var days = s2.getTime() - s1.getTime();
var time = Math.floor(days / (1000 * 60 * 60 * 24));
var Sum = Math.abs(time)
if (Sum<=7) {
return "可退货"
}
if(Sum>=7&&Sum<=14) {
return "可换货"
}
}
}
},