tmp: 部分代码与UnishopV5结合,但是代码有严重缺陷
This commit is contained in:
@@ -1,161 +1,159 @@
|
||||
Function.prototype.asyAfter = function(afterfn) {
|
||||
var _self = this;
|
||||
return function() {
|
||||
var ret = _self.apply(this, arguments);
|
||||
if (ret === 'next') {
|
||||
return afterfn.apply(this, arguments);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
Date.prototype.pattern = function(fmt) {
|
||||
var o = {
|
||||
"M+": this.getMonth() + 1, //月份
|
||||
"d+": this.getDate(), //日
|
||||
"h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
|
||||
"H+": this.getHours(), //小时
|
||||
"m+": this.getMinutes(), //分
|
||||
"s+": this.getSeconds(), //秒
|
||||
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
|
||||
"S": this.getMilliseconds() //毫秒
|
||||
};
|
||||
var week = {
|
||||
"0": "\u65e5",
|
||||
"1": "\u4e00",
|
||||
"2": "\u4e8c",
|
||||
"3": "\u4e09",
|
||||
"4": "\u56db",
|
||||
"5": "\u4e94",
|
||||
"6": "\u516d"
|
||||
};
|
||||
if (/(y+)/.test(fmt)) {
|
||||
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
|
||||
}
|
||||
if (/(E+)/.test(fmt)) {
|
||||
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") :
|
||||
"") +
|
||||
week[this.getDay() + ""]);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
const isType = type => (/^\[object\s(.*)\]$/.exec(Object.prototype.toString.call(type)))[1];
|
||||
let Time = function() {};
|
||||
let timeProto = Time.prototype;
|
||||
|
||||
//获取当前时间戳
|
||||
timeProto.getUnix = function() {
|
||||
return new Date().getTime();
|
||||
}
|
||||
|
||||
//获取当天0点0分0秒时间戳
|
||||
timeProto.getTodayUnix = function() {
|
||||
let date = new Date();
|
||||
let myDate = `${date.getFullYear()}/${(date.getMonth() + 1)}/${date.getDate()} 00:00:00`;
|
||||
return new Date(myDate).getTime();
|
||||
}
|
||||
|
||||
//获取今年1月1日0点0分0秒时间戳
|
||||
timeProto.getYearUnix = function() {
|
||||
let date = new Date();
|
||||
date.setMonth(0);
|
||||
date.setDate(1);
|
||||
date.setHours(0);
|
||||
date.setMinutes(0);
|
||||
date.setSeconds(0);
|
||||
date.setMilliseconds(0);
|
||||
return date.getTime();
|
||||
}
|
||||
|
||||
//获取当前时间标准年月日
|
||||
timeProto.getLastDate = function(constTime) {
|
||||
if (!constTime) {
|
||||
return;
|
||||
}
|
||||
let date = new Date(constTime);
|
||||
if (date.pattern) {
|
||||
return date.pattern("yyyy-MM-dd");
|
||||
}
|
||||
|
||||
let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
|
||||
let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
|
||||
return date.getFullYear() + '-' + month + '-' + day;
|
||||
}
|
||||
|
||||
const resDateStr = function(timer, constTime) {
|
||||
let _just = function(timer) {
|
||||
|
||||
if (timer <= 0 || Math.floor(timer / 60) <= 0) {
|
||||
return "刚刚"
|
||||
} else return 'next';
|
||||
}
|
||||
|
||||
let _mm = function(timer) {
|
||||
if (timer < 3600) {
|
||||
return Math.floor(timer / 60) + "分钟前"
|
||||
} else return 'next';
|
||||
}
|
||||
let _hh = function(timer, constTime) {
|
||||
let today = _time_.getTodayUnix();
|
||||
if (timer >= 3600 && (constTime - today >= 0)) {
|
||||
//可切换显示模式
|
||||
// return "今天 " + new Date(constTime).pattern("HH:mm");
|
||||
return Math.floor(timer / 60 / 60) + "小时前";
|
||||
} else {
|
||||
return 'next'
|
||||
};
|
||||
}
|
||||
let _dd = function(timer, constTime) {
|
||||
let today = _time_.getTodayUnix();
|
||||
timer = (today - constTime) / 1000;
|
||||
if (timer / 86400 <= 31) {
|
||||
return Math.ceil(timer / 86400) + "天前"
|
||||
} else return 'next';
|
||||
}
|
||||
let _dlast = function(timer, constTime) {
|
||||
return _time_.getLastDate(constTime);
|
||||
}
|
||||
|
||||
let dateFilter = _just.asyAfter(_mm).asyAfter(_hh).asyAfter(_dd).asyAfter(_dlast);
|
||||
return dateFilter(timer, constTime);
|
||||
}
|
||||
|
||||
|
||||
//转换时间
|
||||
const reg = new RegExp("-", "g");
|
||||
timeProto.getFormatTime = function(constTime, max) {
|
||||
if (!constTime) {
|
||||
return "";
|
||||
}
|
||||
|
||||
switch (isType(constTime)) {
|
||||
case 'Date':
|
||||
constTime = constTime.getTime();
|
||||
break;
|
||||
case 'String':
|
||||
constTime = constTime.replace(reg, "/");
|
||||
default:
|
||||
constTime = new Date(constTime).getTime();
|
||||
break;
|
||||
}
|
||||
|
||||
let now = this.getUnix();
|
||||
let year = this.getYearUnix();
|
||||
let timer = (now - constTime) / 1000;
|
||||
if (constTime > now && max) {
|
||||
return this.getLastDate(constTime);
|
||||
}
|
||||
|
||||
let _t = this;
|
||||
return resDateStr(timer, constTime);
|
||||
}
|
||||
|
||||
const _time_ = new Time();
|
||||
export default _time_;
|
||||
Function.prototype.asyAfter = function(afterfn) {
|
||||
var _self = this;
|
||||
return function() {
|
||||
var ret = _self.apply(this, arguments);
|
||||
if (ret === 'next') {
|
||||
return afterfn.apply(this, arguments);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
Date.prototype.pattern = function(fmt) {
|
||||
var o = {
|
||||
"M+": this.getMonth() + 1, //月份
|
||||
"d+": this.getDate(), //日
|
||||
"h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
|
||||
"H+": this.getHours(), //小时
|
||||
"m+": this.getMinutes(), //分
|
||||
"s+": this.getSeconds(), //秒
|
||||
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
|
||||
"S": this.getMilliseconds() //毫秒
|
||||
};
|
||||
var week = {
|
||||
"0": "\u65e5",
|
||||
"1": "\u4e00",
|
||||
"2": "\u4e8c",
|
||||
"3": "\u4e09",
|
||||
"4": "\u56db",
|
||||
"5": "\u4e94",
|
||||
"6": "\u516d"
|
||||
};
|
||||
if (/(y+)/.test(fmt)) {
|
||||
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
|
||||
}
|
||||
if (/(E+)/.test(fmt)) {
|
||||
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
const isType = type => (/^\[object\s(.*)\]$/.exec(Object.prototype.toString.call(type)))[1];
|
||||
let Time = function() {};
|
||||
let timeProto = Time.prototype;
|
||||
|
||||
//获取当前时间戳
|
||||
timeProto.getUnix = function() {
|
||||
return new Date().getTime();
|
||||
}
|
||||
|
||||
//获取当天0点0分0秒时间戳
|
||||
timeProto.getTodayUnix = function() {
|
||||
let date = new Date();
|
||||
let myDate = `${date.getFullYear()}/${(date.getMonth() + 1)}/${date.getDate()} 00:00:00`;
|
||||
return new Date(myDate).getTime();
|
||||
}
|
||||
|
||||
//获取今年1月1日0点0分0秒时间戳
|
||||
timeProto.getYearUnix = function() {
|
||||
let date = new Date();
|
||||
date.setMonth(0);
|
||||
date.setDate(1);
|
||||
date.setHours(0);
|
||||
date.setMinutes(0);
|
||||
date.setSeconds(0);
|
||||
date.setMilliseconds(0);
|
||||
return date.getTime();
|
||||
}
|
||||
|
||||
//获取当前时间标准年月日
|
||||
timeProto.getLastDate = function(constTime) {
|
||||
if (!constTime) {
|
||||
return;
|
||||
}
|
||||
let date = new Date(constTime);
|
||||
if (date.pattern) {
|
||||
return date.pattern("yyyy-MM-dd");
|
||||
}
|
||||
|
||||
let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
|
||||
let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
|
||||
return date.getFullYear() + '-' + month + '-' + day;
|
||||
}
|
||||
|
||||
const resDateStr = function(timer, constTime) {
|
||||
let _just = function(timer) {
|
||||
|
||||
if (timer <= 0 || Math.floor(timer / 60) <= 0) {
|
||||
return "刚刚"
|
||||
} else return 'next';
|
||||
}
|
||||
|
||||
let _mm = function(timer) {
|
||||
if (timer < 3600) {
|
||||
return Math.floor(timer / 60) + "分钟前"
|
||||
} else return 'next';
|
||||
}
|
||||
let _hh = function(timer, constTime) {
|
||||
let today = _time_.getTodayUnix();
|
||||
if (timer >= 3600 && (constTime - today >= 0)) {
|
||||
//可切换显示模式
|
||||
// return "今天 " + new Date(constTime).pattern("HH:mm");
|
||||
return Math.floor(timer / 60 / 60) + "小时前";
|
||||
} else {
|
||||
return 'next'
|
||||
}
|
||||
}
|
||||
let _dd = function(timer, constTime) {
|
||||
let today = _time_.getTodayUnix();
|
||||
timer = (today - constTime) / 1000;
|
||||
if (timer / 86400 <= 31) {
|
||||
return Math.ceil(timer / 86400) + "天前"
|
||||
} else return 'next';
|
||||
}
|
||||
let _dlast = function(timer, constTime) {
|
||||
return _time_.getLastDate(constTime);
|
||||
}
|
||||
|
||||
let dateFilter = _just.asyAfter(_mm).asyAfter(_hh).asyAfter(_dd).asyAfter(_dlast);
|
||||
return dateFilter(timer, constTime);
|
||||
}
|
||||
|
||||
|
||||
//转换时间
|
||||
const reg = new RegExp("-", "g");
|
||||
timeProto.getFormatTime = function(constTime, max) {
|
||||
if (!constTime) {
|
||||
return "";
|
||||
}
|
||||
|
||||
switch (isType(constTime)) {
|
||||
case 'Date':
|
||||
constTime = constTime.getTime();
|
||||
break;
|
||||
case 'String':
|
||||
constTime = constTime.replace(reg, "/");
|
||||
break;
|
||||
default:
|
||||
constTime = new Date(constTime).getTime();
|
||||
break;
|
||||
}
|
||||
|
||||
let now = this.getUnix();
|
||||
let year = this.getYearUnix();
|
||||
let timer = (now - constTime) / 1000;
|
||||
if (constTime > now && max) {
|
||||
return this.getLastDate(constTime);
|
||||
}
|
||||
|
||||
let _t = this;
|
||||
return resDateStr(timer, constTime);
|
||||
}
|
||||
|
||||
const _time_ = new Time();
|
||||
export default _time_;
|
||||
|
||||
@@ -1,98 +1,97 @@
|
||||
//字符串拼接
|
||||
export function strFormat(str) {
|
||||
return str < 10 ? `0${str}` : str
|
||||
}
|
||||
// 获取当前时间
|
||||
export function currentTime() {
|
||||
const myDate = new Date();
|
||||
const year = myDate.getFullYear()
|
||||
const m = myDate.getMonth() + 1;
|
||||
const d = myDate.getDate();
|
||||
// const date = year + '-' + strFormat(m) + '-' + strFormat(d); // 隐藏年
|
||||
const date = strFormat(m) + '-' + strFormat(d);
|
||||
|
||||
const hour = myDate.getHours()
|
||||
const min = myDate.getMinutes()
|
||||
const secon = myDate.getSeconds()
|
||||
const time = strFormat(hour) + ':' + strFormat(min) + ':' + strFormat(secon);
|
||||
return {
|
||||
year,
|
||||
date,
|
||||
time
|
||||
}
|
||||
}
|
||||
|
||||
//时间戳转日期
|
||||
export function timeStamp(time) {
|
||||
const dates = new Date(time)
|
||||
const year = dates.getFullYear()
|
||||
const month = dates.getMonth() + 1
|
||||
const date = dates.getDate()
|
||||
const day = dates.getDay()
|
||||
const hour = dates.getHours()
|
||||
const min = dates.getMinutes()
|
||||
const days = ['日', '一', '二', '三', '四', '五', '六']
|
||||
return {
|
||||
allDate: `${year}/${strFormat(month)}/${strFormat(date)}`,
|
||||
date: `${strFormat(month)}-${strFormat(date)}`, //返回的日期 07-01,${strFormat(year)}-${strFormat(month)}-${strFormat(date)}
|
||||
day: `周${days[day]}`, //返回的礼拜天数 星期一
|
||||
hour: strFormat(hour) + ':' + strFormat(min) // + ':00' //返回的时钟 08:00
|
||||
}
|
||||
}
|
||||
|
||||
//获取最近7天的日期和礼拜天数
|
||||
export function initData(appointedDay = '') {
|
||||
const time = []
|
||||
const date = appointedDay ? new Date(appointedDay) : new Date()
|
||||
|
||||
const now = date.getTime() //获取当前日期的时间戳
|
||||
let timeStr = 3600 * 24 * 1000 //一天的时间戳
|
||||
let obj = {
|
||||
0: "今天",
|
||||
1: "明天",
|
||||
2: "后天"
|
||||
}
|
||||
for (let i = 0; i < 7; i++) {
|
||||
const timeObj = {}
|
||||
timeObj.date = timeStamp(now + timeStr * i).date //保存日期
|
||||
timeObj.timeStamp = now + timeStr * i //保存时间戳
|
||||
timeObj.week = appointedDay == '' ? (obj[i] ?? timeStamp(now + timeStr * i).day) : timeStamp(now + timeStr * i)
|
||||
.day
|
||||
time.push(timeObj)
|
||||
}
|
||||
return time
|
||||
}
|
||||
|
||||
//时间数组
|
||||
export function initTime(startTime = '09:00', endTime = '18:30', timeInterval = 1) {
|
||||
const time = []
|
||||
const date = timeStamp(Date.now()).allDate
|
||||
const startDate = `${date} ${startTime}`
|
||||
const endDate = `${date} ${endTime}`
|
||||
const startTimeStamp = new Date(startDate).getTime()
|
||||
const endTimeStamp = new Date(endDate).getTime()
|
||||
const timeStr = 3600 * 1000 * timeInterval
|
||||
for (let i = startTimeStamp; i <= endTimeStamp; i = i + timeStr) {
|
||||
const timeObj = {}
|
||||
timeObj.time = timeStamp(i).hour
|
||||
timeObj.disable = false
|
||||
time.push(timeObj)
|
||||
}
|
||||
return time
|
||||
}
|
||||
|
||||
export function weekDate(){
|
||||
var now = new Date(); //当前日期
|
||||
var nowDayOfWeek = now.getDay(); //今天本周的第几天
|
||||
var nowDay = now.getDate(); //当前日
|
||||
var nowMonth = now.getMonth(); //当前月
|
||||
var nowYear = now.getYear(); //当前年
|
||||
|
||||
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
|
||||
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek));
|
||||
|
||||
var arr = [];
|
||||
arr[0] = strFormat(weekStartDate.getMonth()+1) + '-' + strFormat(weekStartDate.getDate());
|
||||
arr[1] = strFormat(weekEndDate.getMonth()+1) + '-' + strFormat(weekEndDate.getDate());
|
||||
return arr;
|
||||
}
|
||||
//字符串拼接
|
||||
export function strFormat(str) {
|
||||
return str < 10 ? `0${str}` : str
|
||||
}
|
||||
// 获取当前时间
|
||||
export function currentTime() {
|
||||
const myDate = new Date();
|
||||
const year = myDate.getFullYear()
|
||||
const m = myDate.getMonth() + 1;
|
||||
const d = myDate.getDate();
|
||||
// const date = year + '-' + strFormat(m) + '-' + strFormat(d); // 隐藏年
|
||||
const date = strFormat(m) + '-' + strFormat(d);
|
||||
|
||||
const hour = myDate.getHours()
|
||||
const min = myDate.getMinutes()
|
||||
const secon = myDate.getSeconds()
|
||||
const time = strFormat(hour) + ':' + strFormat(min) + ':' + strFormat(secon);
|
||||
return {
|
||||
year,
|
||||
date,
|
||||
time
|
||||
}
|
||||
}
|
||||
|
||||
//时间戳转日期
|
||||
export function timeStamp(time) {
|
||||
const dates = new Date(time)
|
||||
const year = dates.getFullYear()
|
||||
const month = dates.getMonth() + 1
|
||||
const date = dates.getDate()
|
||||
const day = dates.getDay()
|
||||
const hour = dates.getHours()
|
||||
const min = dates.getMinutes()
|
||||
const days = ['日', '一', '二', '三', '四', '五', '六']
|
||||
return {
|
||||
allDate: `${year}/${strFormat(month)}/${strFormat(date)}`,
|
||||
date: `${strFormat(month)}-${strFormat(date)}`, //返回的日期 07-01,${strFormat(year)}-${strFormat(month)}-${strFormat(date)}
|
||||
day: `周${days[day]}`, //返回的礼拜天数 星期一
|
||||
hour: strFormat(hour) + ':' + strFormat(min) // + ':00' //返回的时钟 08:00
|
||||
}
|
||||
}
|
||||
|
||||
//获取最近7天的日期和礼拜天数
|
||||
export function initData(appointedDay = '') {
|
||||
const time = []
|
||||
const date = appointedDay ? new Date(appointedDay) : new Date()
|
||||
|
||||
const now = date.getTime() //获取当前日期的时间戳
|
||||
let timeStr = 3600 * 24 * 1000 //一天的时间戳
|
||||
let obj = {
|
||||
0: "今天",
|
||||
1: "明天",
|
||||
2: "后天"
|
||||
}
|
||||
for (let i = 0; i < 7; i++) {
|
||||
const timeObj = {}
|
||||
timeObj.date = timeStamp(now + timeStr * i).date //保存日期
|
||||
timeObj.timeStamp = now + timeStr * i //保存时间戳
|
||||
timeObj.week = appointedDay == '' ? (obj[i] ?? timeStamp(now + timeStr * i).day) : timeStamp(now + timeStr * i).day
|
||||
time.push(timeObj)
|
||||
}
|
||||
return time
|
||||
}
|
||||
|
||||
//时间数组
|
||||
export function initTime(startTime = '09:00', endTime = '18:30', timeInterval = 1) {
|
||||
const time = []
|
||||
const date = timeStamp(Date.now()).allDate
|
||||
const startDate = `${date} ${startTime}`
|
||||
const endDate = `${date} ${endTime}`
|
||||
const startTimeStamp = new Date(startDate).getTime()
|
||||
const endTimeStamp = new Date(endDate).getTime()
|
||||
const timeStr = 3600 * 1000 * timeInterval
|
||||
for (let i = startTimeStamp; i <= endTimeStamp; i = i + timeStr) {
|
||||
const timeObj = {}
|
||||
timeObj.time = timeStamp(i).hour
|
||||
timeObj.disable = false
|
||||
time.push(timeObj)
|
||||
}
|
||||
return time
|
||||
}
|
||||
|
||||
export function weekDate(){
|
||||
var now = new Date(); //当前日期
|
||||
var nowDayOfWeek = now.getDay(); //今天本周的第几天
|
||||
var nowDay = now.getDate(); //当前日
|
||||
var nowMonth = now.getMonth(); //当前月
|
||||
var nowYear = now.getYear(); //当前年
|
||||
|
||||
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
|
||||
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek));
|
||||
|
||||
var arr = [];
|
||||
arr[0] = strFormat(weekStartDate.getMonth()+1) + '-' + strFormat(weekStartDate.getDate());
|
||||
arr[1] = strFormat(weekEndDate.getMonth()+1) + '-' + strFormat(weekEndDate.getDate());
|
||||
return arr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user