get-time-types
Version:
获取时间工具
153 lines (152 loc) • 8.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
// 获取时间的类
var GetTime = /** @class */ (function () {
// 构造函数
function GetTime(getType, returnType, value, defaultData) {
this.getType = getType;
this.returnType = returnType;
this.value = value;
this.defaultData = defaultData;
}
GetTime.prototype.filterMoment = function () {
var _this = this;
//判断默认时间是否传递
var dateTime;
this.defaultData == 'default' ? dateTime = new Date() : dateTime = new Date(this.defaultData);
//根据getType不同类型处理 value + getType 的时间。
var typeArray = {
//(获取到前/后value年的时间。并传给getMoment函数处理)
'yyyy': function () {
dateTime.setFullYear(dateTime.getFullYear() + _this.value);
var curryDate = _this.getMoment(dateTime, _this.returnType);
return curryDate;
},
//(获取到前/后value月的时间。并传给getMoment函数处理)
'MM': function () {
dateTime.setMonth(dateTime.getMonth() + _this.value);
var curryDate = _this.getMoment(dateTime, _this.returnType);
return curryDate;
},
//(获取到前/后this.value日的时间。并传给this.getMoment函数处理)
'dd': function () {
dateTime.setDate(dateTime.getDate() + _this.value);
var curryDate = _this.getMoment(dateTime, _this.returnType);
return curryDate;
},
//(获取到前/后this.value小时的时间。并传给this.getMoment函数处理)
'HH': function () {
dateTime.setHours(dateTime.getHours() + _this.value);
var curryDate = _this.getMoment(dateTime, _this.returnType);
return curryDate;
},
//(获取到前/后this.value分钟的时间。并传给this.getMoment函数处理)
'mm': function () {
dateTime.setMinutes(dateTime.getMinutes() + _this.value);
var curryDate = _this.getMoment(dateTime, _this.returnType);
return curryDate;
},
//(获取到前/后this.value秒钟的时间。并传给this.getMoment函数处理)
'ss': function () {
dateTime.setSeconds(dateTime.getSeconds() + _this.value);
var curryDate = _this.getMoment(dateTime, _this.returnType);
return curryDate;
},
//不传递类型,则返回yyyy-MM-dd HH:mm:ss格式的当前时间
'default': function () {
var curryDate = _this.getMoment(dateTime, _this.returnType);
return curryDate;
}
};
try {
return typeArray[this.getType]();
}
catch (error) {
throw new Error('No such getType');
}
};
// 根据默认时间和返回时间类型去获取时间
GetTime.prototype.getMoment = function (defaultData, returnType) {
//第一步工作,拿到传入该函数的时间value
var dateTime = defaultData;
//第二步工作,分别获取传入时间的年,月,日,时,分,秒,星期,yyyy-MM-dd,HH:mm:ss,yyyy-MM-dd HH:mm:ss格式的值
var year = dateTime.getFullYear(); //当前年
var month = dateTime.getMonth() + 1;
var date = dateTime.getDate();
var hours = dateTime.getHours();
var minutes = dateTime.getMinutes();
var seconds = dateTime.getSeconds();
var week = dateTime.getDay();
month = month < 10 ? '0' + month : month; //当前月
date = date < 10 ? '0' + date : date; //当前日
hours = hours < 10 ? '0' + hours : hours; //当前小时
minutes = minutes < 10 ? '0' + minutes : minutes; //当前分钟
seconds = seconds < 10 ? '0' + seconds : seconds; //当前秒钟
week == 0 ? '日' : week; //当前星期
var curry_year_month = year + '-' + month; //年-月
var curry_month_date = month + '-' + date; //月-日
var curry_year_month_date = year + '-' + month + '-' + date; //年-月-日
var curry_year_month_date_hours = year + '-' + month + '-' + date + ' ' + hours; //年-月-日 时
var curry_year_month_date_hours_minutes = year + '-' + month + '-' + date + ' ' + hours + ':' + minutes; //年-月-日 时:分
var curry_year_month_date_hours_minutes_seconds = year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + seconds; //年-月-日 时:分:秒
var curry_month_date_hours_minutes_seconds = month + '-' + date + ' ' + hours + ':' + minutes + ':' + seconds; //月-日 时:分:秒
var curry_month_date_hours_minutes = month + '-' + date + ' ' + hours + ':' + minutes; //月-日 时:分
var curry_month_date_hours = month + '-' + date + ' ' + hours; //月-日 时
var curry_date_hours_minutes_seconds = date + ' ' + hours + ':' + minutes + ':' + seconds; //日 时:分:秒
var curry_date_hours_minutes = date + ' ' + hours + ':' + minutes; //日 时:分
var curry_date_hours = date + ' ' + hours; //日 时
var curry_hours_minutes_seconds = hours + ':' + minutes + ':' + seconds; //时:分:秒
var curry_hours_minutes = hours + ':' + minutes; //时:分
var curry_minutes_seconds = hours + ':' + minutes + ':' + seconds; //分:秒
//第三步工作,根据传入类型判断要返回的值,这里为了避免大量if判断。采用了策略模式。
var returnTypeList = {
'yyyy': function () { return year; },
'MM': function () { return month; },
'dd': function () { return date; },
'HH': function () { return hours; },
'mm': function () { return minutes; },
'ss': function () { return seconds; },
'week': function () {
var weekArr = {
0: function () { return '日'; },
1: function () { return '一'; },
2: function () { return '二'; },
3: function () { return '三'; },
4: function () { return '四'; },
5: function () { return '五'; },
6: function () { return '六'; }
};
return weekArr[week] ? weekArr[week]() : 'no';
},
'yyyy-MM': function () { return curry_year_month; },
'MM-dd': function () { return curry_month_date; },
'yyyy-MM-dd': function () { return curry_year_month_date; },
'yyyy-MM-dd HH': function () { return curry_year_month_date_hours; },
'yyyy-MM-dd HH:mm': function () { return curry_year_month_date_hours_minutes; },
'yyyy-MM-dd HH:mm:ss': function () { return curry_year_month_date_hours_minutes_seconds; },
'MM-dd HH:mm:ss': function () { return curry_month_date_hours_minutes_seconds; },
'MM-dd HH:mm': function () { return curry_month_date_hours_minutes; },
'MM-dd HH': function () { return curry_month_date_hours; },
'dd HH:mm:ss': function () { return curry_date_hours_minutes_seconds; },
'dd HH:mm': function () { return curry_date_hours_minutes; },
'dd HH': function () { return curry_date_hours; },
'HH:mm:ss': function () { return curry_hours_minutes_seconds; },
'HH:mm': function () { return curry_hours_minutes; },
'mm:ss': function () { return curry_minutes_seconds; },
'default': function () { return curry_year_month_date_hours_minutes_seconds; },
};
try {
return returnTypeList[returnType]();
}
catch (error) {
throw Error('No such returnType');
}
};
return GetTime;
}());
function getTime(params) {
var getTime = new GetTime(params.getType ? params.getType : 'default', params.returnType ? params.returnType : 'default', params.value ? params.value : 0, params.defaultData ? params.defaultData : 'default');
var thisTime = getTime.filterMoment();
return thisTime;
}
exports.default = getTime;